oracle中,表A中共有a,b,c三个字段,c初始为0,只要a或b发生改变,那么c值变为1
表A
a b c
1 1 0
1 2 0
1 3 0
如果
表A数据改变,希望对变化数据添加标识,即c变为1
a  b c  
1 1  0
2  2 1
3  3 1 
各位大神,在oracle 中该怎么实现呢???

解决方案 »

  1.   

    利用触发器,思路如下:
    create or replace trigger is tri_name 
    after update on  tableName
    for each row--这个一般都要加上,因为我们一般都是行级触发器,即对每一行都操作
    declare  --若没有变量,则可以省略declarebegin
    if :new.a<>:old.a or :new.b<>:old.b then
            update tableName t set t.c=1 where t.a=:old.a and t.b=:old.b;
        end if;
           
    end;
      

  2.   

    少写了一点,需要声明为自定义事务,如下:create or replace trigger is tri_name 
    after update on  tableName
    for each row--这个一般都要加上,因为我们一般都是行级触发器,即对每一行都操作
    declare  --在变量申明的地方,指定自定义事务处理。
     pragma autonomous_transaction; 
    begin
    if :new.a<>:old.a or :new.b<>:old.b then
            update tableName t set t.c=1 where t.a=:old.a and t.b=:old.b;
    commit;
        end if;       
    end;     
      

  3.   

    create or replace trigger 触发器名
    before update on 表名
    for each row
    when (old.a!=new.a or old.b!=new.b)
    begin
      :new.c:=1;
    end;
    /
      

  4.   

    这个不能使用自主事务,也不能使用 commit ,更不能使用update 更新本表。3# 的语法是对的;