例如表:test_func 
    code      codeName        superId     isref    
    000        北京一小学      0          0 
    000001    一年级          000         0 
    000001101  一年级一班      000001     1 
    000001102  一年级二班      000001     1 
    000001103  一年级三班      000001     1 
    000002    二年级          000         1 
功能:插入(0000002,二年级一班,000002,1)时,上面二年级的那条记录isref值要变为0,因为插入后它就不是叶子结点了。

解决方案 »

  1.   

    create or replace function f_update_leaf(
      i_code varchar2,
      i_codeName varchar2,
      i_superID varchar2,
      i_isref number)
    return number
    is
    begin
            insert into test_func values(i_code,i_codeName, i_superID,i_isref);
            update test_func set isref=0 where code=i_codeName;
            if sql%notfound then
                     null;
            end;
            return 1;
            exception when others then
            return 0;
    end;
    /
      

  2.   

    你的字段是varchar2的?
    update test_func set isref=0 where code=i_codeName; 
    ->
    update test_func set isref=0 where trim(code)=trim(i_codeName); 
      

  3.   

    我承认,是我没有测试,end后面少了个 if;SQL> create table test_func(
      2      code      varchar2(10),
      3      codeName       varchar2(100),
      4      superId    varchar2(10),
      5      isref      number
      6  );表已创建。SQL> insert into test_func values('000002','二年级','000',        1 );已创建 1 行。SQL>
    SQL> create or replace function f_update_leaf(
      2    i_code in varchar2,
      3    i_codeName in varchar2,
      4    i_superID in varchar2,
      5    i_isref in number)
      6  return number
      7  is
      8  begin
      9          insert into test_func values(i_code,i_codeName, i_superID,i_isref);
     10          update test_func set isref=0 where code=i_codeName;
     11          if sql%notfound then
     12                  null;
     13          end if;
     14          return 1;
     15          exception when others then
     16          raise;
     17  end;
     18  /函数已创建。SQL>
    SQL>  declare f number;
      2   begin
      3   f:=f_update_leaf('0000002','二年机一般','000002',1);
      4   end;
      5  /PL/SQL 过程已成功完成。SQL>
    SQL>  select * from test_func;CODE
    --------------------
    CODENAME
    --------------------------------------------------------------------------------
    SUPERID                   ISREF
    -------------------- ----------
    000002
    二年级
    000                           10000002
    二年机一般
    000002                        1CODE
    --------------------
    CODENAME
    --------------------------------------------------------------------------------
    SUPERID                   ISREF
    -------------------- ----------
      

  4.   


    drop table test_func;
    create table test_func(
        code      varchar2(10),
        codeName       varchar2(100),
        superId    varchar2(10),
        isref      number
    );
    insert into test_func values('000002','二年级','000',        1 );create or replace function f_update_leaf(
      i_code in varchar2,
      i_codeName in varchar2,
      i_superID in varchar2,
      i_isref in number)
    return number
    is
    begin
            insert into test_func values(i_code,i_codeName, i_superID,i_isref);
            update test_func set isref=0 where trim(code)=trim(i_superID);
            if sql%notfound then
                    null;
            end if;
            return 1;
            exception when others then
            raise;
    end;
    / declare f number;
     begin
     f:=f_update_leaf('0000002','二年机一般','000002',1);
     end;
    / select * from test_func;
      

  5.   

    看5楼的,测试过了。
    上面update的条件搞成了name,所以是不对的。。
      

  6.   

    学习
    问下sql%notfound  这表示什么意思
    没看到有sql变量啊