各位,初级问题,关于Oracle触发器的问题,谢谢!
tableA
A_ID  A_NAME A_STATUStableB
B_ID  B_NAME B_STATUS
当在tableA中将A_STATUS改为3的时候,将当前记录添加到tableB,请问这个触发器如何写呢,谢谢!根据详细情况给分。

解决方案 »

  1.   

    create or replace trigger tg_tablea
    after update on tablea
    for each row 
    when (new.a_status=3)
    begin
    insert into tableb values(:new.a_id,:new.a_name,:new.a_status);
    end;
      

  2.   

    create or replace trigger t_a
      before insert on ssh_a  
      for each row
    DECLARE
      -- local variables here
    BEGIN  IF :NEW.A_NAME = '3' THEN
        INSERT INTO SSH_B VALUES (:NEW.ID, :NEW.A_NAME);
      END IF;
    END T_A;
      

  3.   

    create or replace trigger tri_test after update on tableA for each row
    when (new.A_status = 3)
    begin
    insert into tableB values (:old.A_id,:old.A_name,:old.A_status);
    end;
    /
      

  4.   

    11:22:30 scott@TUNGKONG> select * from tableA;      A_ID A_NAME       A_STATUS
    ---------- ---------- ----------
             1 test1               1
             2 test2               2已用时间:  00: 00: 00.01
    11:22:35 scott@TUNGKONG> select * from tableB;未选定行已用时间:  00: 00: 00.00
    11:23:00 scott@TUNGKONG> update tableA set A_status = 2 where A_id = 1;已更新 1 行。已用时间:  00: 00: 00.00
    11:23:09 scott@TUNGKONG> select * from tableB;未选定行已用时间:  00: 00: 00.00
    11:23:16 scott@TUNGKONG> update tableA set A_status = 3 where A_id = 2;已更新 1 行。已用时间:  00: 00: 00.01
    11:23:33 scott@TUNGKONG> select * from tableA;      A_ID A_NAME       A_STATUS
    ---------- ---------- ----------
             1 test1               2
             2 test2               3已用时间:  00: 00: 00.01
    11:23:36 scott@TUNGKONG> select * from tableB;      B_ID B_NAME       B_STATUS
    ---------- ---------- ----------
             2 test2               2已用时间:  00: 00: 00.01
      

  5.   

    内容补充:
    还有tableC
    C_ID  C_DESCRIPTION
    和tableD
    D_ID  D_DESCRIPTION
    A_ID与C_ID关联,当删除tableA中的记录时,将当前记录添加到tableB,同时删除tableC,
    同时将tableC中当前记录添加到tableD,谢谢
      

  6.   

    create or replace trigger tg_tablea 
    after update or delete on tablea 
    for each row 
    begin 
    if updating then
     .... end if;
    if deleting then 
      .... end if;
    end;
    了解下触发器的语法,结构和:new,:old的用法
    剩下的在里面添加insert delete操作都很简单
      

  7.   

    Create Or Replace Trigger tr_TableA
     Before Insert on TableA For Each Row
     Declare
      --Local Variables Here
     Begin
      If :New.A_STATUS='3' then 
      Insert Into TableB values( :New.A_ID, :New.A_NAME , :New.A_STATUS);
    End If;
    End Tr_TableA;
      

  8.   

    create or replace trigger tg_tablea 
    after update or delete on tablea 
    for each row 
    begin 
    if updating and :new.a_status=3 then 
    insert into tableb values(:old.a_id,:old.a_name,:old.a_status);  end if; 
    if deleting then 
     insert into tableb values(:old.a_id,:old.a_name,:old.a_status);
      delete from tablec where c_id=:old.a_id; end if; 
    end; 
    再对c表建一个触发器
    create or replace trigger tg_tablec
    after delete on tablec
    for each row 
    begin 
    insert into tabled values(:old.c_id,:old.c_name,:old.c_status);
    end; 
    试试