有两个表:
表1 card_info (CARD_NO,xm,xb,hj) 其中card_no 是主键  (主表)
表2 card_charge_record (autoid,CARD_NO,XM,je)  autoid 是主键 (这个表是存明细的)
现在是想当表1中 card_no 号改变时即时更新表2 使其与表1同步
CREATE OR REPLACE TRIGGER card_info_update_card_no
  AFTER UPDATE of card_no
  ON card_info
  FOR EACH ROW
  when  (new.card_no<> old.card_no) 
  DECLARE
 --   ls_pm   mzfymx.pm%type:='';
  
BEGIN
 update card_charge_record set card_no = :new.card_no  where CARD_NO = :old.card_no;
END;
/
触发器编译正常但,就是不能改变表2的值

解决方案 »

  1.   


    drop table t1;
    drop table t2;
    create table t1(id int,name varchar2(10));
    create table t2(id int,name varchar2(10));
    insert into t1 values(1,'1');
    insert into t1 values(2,'2');
    insert into t1 values(3,'3');
    insert into t2 values(1,'1');
    insert into t2 values(1,'2');
    insert into t2 values(1,'3');
    commit;
    select * from t1;
                                         ID NAME
    --------------------------------------- ----------
                                          1 1
                                          2 2
                                          3 3
                                          
    select * from t2;
                                         ID NAME
    --------------------------------------- ----------
                                          1 1
                                          1 2
                                          1 3
    create or replace trigger tri_t1_update
      after update of id on t1
      for each row
      when (new.id <> old.id)
    begin
      update t2 set t2.id = :new.id where t2.id = :old.id;
    end;
    /update t1 set id=4 where id=1;   --更新id=1的记录
    commit;
    select * from t1;
                                         ID NAME
    --------------------------------------- ----------
                                          4 1
                                          2 2
                                          3 3
    select * from t2;
                                         ID NAME
    --------------------------------------- ----------
                                          4 1
                                          4 2
                                          4 3