1 --建表: 2 create table zhidao_20131014_tab2_1 3 ( 4 HEAD_ID varchar2(10), 5 LINE_ID varchar2(10), 6 QTY number 7 ); 8 create table zhidao_20131014_tab2_2 9 (10 HEAD_ID varchar2(10),11 SUMQTY number12 );13 --造数:14 insert into zhidao_20131014_tab2_115 select 'H1','1',100 from dual16 union all17 select 'H1','2',200 from dual18 union all19 select 'H1','2',300 from dual20 union all21 select 'H1','2',100 from dual;22 23 insert into zhidao_20131014_tab2_224 select 'H1',700 from dual;25 commit;26 --触发器:27 create or replace trigger tr_zhidao28 after insert or update or delete29 on zhidao_20131014_tab2_130 for each row31 begin32 case33 when updating then34 update zhidao_20131014_tab2_2 t set SUMQTY=SUMQTY - :o1ld.QTY + :new.QTY where t.head_id=:new.head_id;35 when inserting then36 update zhidao_20131014_tab2_2 t set SUMQTY=SUMQTY + :new.QTY where t.head_id=:new.head_id;37 when deleting then38 update zhidao_20131014_tab2_2 t set SUMQTY=SUMQTY - :old.QTY where t.head_id=:old.head_id;39 end case;40 end;41 --测试:42 --insert43 insert into zhidao_20131014_tab2_1 44 select 'H1','3',300 from dual;45 --delete46 delete zhidao_20131014_tab2_1 where HEAD_ID='H1' and LINE_ID='3';47 --update48 update zhidao_20131014_tab2_1 set QTY=1000 where QTY=300;