SQL: update 表1 set 表1.字段1=XX,表1.字段2=XX;我可以写个触发器,当update表1中的“字段1”时,我就更新另外一个表的字段3吗?
表1有很多字段,但是只有update字段1的时候才触发。请问这个触发器该怎么写?

解决方案 »

  1.   

    --使用updating判断
    create or replace trigger test_trig 
    before update on 表1
    begin
       if updating('字段1') then
          update 表2 set 字段3='<值>';
       end if;
       if updating('字段2') then
          dbms_output.put_line('更新字段2');
       end if;
    end;
      

  2.   

    create or replace trigger tri
      after update of 字段1 on 表1  
      for each row
    declare
      -- local variables here
    begin
      update 表2 set 字段3 =  :new.字段1;
    end tri;
      

  3.   

    create or replace trigger test_trig 
    before update on t1
    begin
       if updating(‘a1‘) then
          dbms_output.put_line(‘updating a1‘);
       end if;
       if updating(‘b1‘) then
          dbms_output.put_line(‘updating b1‘);
       end if;
    end;SQL> update t1 set a1=9 ;
    updating a11 row updated.SQL> update t1 set b1=9 ;
    updating b11 row updated.
      

  4.   

    create or replace trigger tri
      after update of 字段1 on 表1   
      for each row
    declare
      -- local variables here
    begin
      update 表2 set 字段3 = :new.字段1;
    end tri;
      

  5.   

    可以这样写吗?
    create or replace trigger tri
      after update of 字段1,字段2 on 表1   
      for each row
    begin
      update 表2 set 字段3 = :new.字段1,表3.字段3 = :new.字段2
    end;就是:update of XX1,XX2 on table
      

  6.   

    after update of 字段1,字段2 on 表1
    这个是可以的update 表2 set 字段3 = :new.字段1,表3.字段3 = :new.字段2
    这个是不行的,不能在一个语句处指定两张表的字段
      

  7.   

    那能这样啊  写2条update嘛
      

  8.   

    谢谢楼上的,那个表3是我写错了。呵呵。
    还有个问题:
    CREATE OR REPLACE TRIGGER X.UP_TO_BILL
    AFTER UPDATE OF VFIRSTCODE2,CFIRSTID2
    FOR EACH ROW 
    begin
    --if(:new.dreqdate != null)
    --then
    update to_bill
    set dbilldate = :new.dreqdate
    where vcode = :new.vfirstcode2;
    --end if;
    end;
    这个是成功的。
    但是,我把if(:new.dreqdate != null)启用,就不行。:new.dreqdate 是有值的。:new.dreqdate != null这么判断不行吗?
      

  9.   


    CREATE OR REPLACE TRIGGER X.UP_TO_BILL
    AFTER UPDATE OF VFIRSTCODE2,CFIRSTID2
    ON X.PO_REQUIREAPP_B
    REFERENCING OLD AS OLD NEW AS NEW
    FOR EACH ROW 
    begin
    if(:new.dreqdate != null)
    then
    update to_bill
    set dbilldate = :new.dreqdate
    where vcode = :new.vfirstcode2;
    end if;
    end;这样就不行,把if判断去掉就可以。
      

  10.   

    改为  if :new.dreqdate is not null then 式下
      

  11.   

    对,对。
    null的判断写错了
    呵呵
    我要是加两个判断呢?
    if ((:new.dreqdate is not null)&&(:new.VFIRSTCODE2 is not null))
    then 
    这些行吗?
      

  12.   

    if ((:new.dreqdate is not null)and(:new.VFIRSTCODE2 is not null))
    then  
    哈哈,可以。
      

  13.   

    呵呵  要么and 要么or