oracle触发器例子

 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  number
12 );
13 --造数:
14 insert into zhidao_20131014_tab2_1
15 select 'H1','1',100 from dual
16 union all
17 select 'H1','2',200 from dual
18 union all
19 select 'H1','2',300 from dual
20 union all
21 select 'H1','2',100 from dual;
22  
23 insert into zhidao_20131014_tab2_2
24 select 'H1',700 from dual;
25 commit;
26 --触发器:
27 create or replace trigger tr_zhidao
28 after insert or update or delete
29 on zhidao_20131014_tab2_1
30 for each row
31 begin
32   case
33     when updating then
34       update zhidao_20131014_tab2_2 t set SUMQTY=SUMQTY - :o1ld.QTY + :new.QTY where t.head_id=:new.head_id;
35     when inserting then
36       update zhidao_20131014_tab2_2 t set SUMQTY=SUMQTY + :new.QTY where t.head_id=:new.head_id;
37     when deleting then
38       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 --insert
43 insert into zhidao_20131014_tab2_1 
44 select 'H1','3',300 from dual;
45 --delete
46 delete zhidao_20131014_tab2_1 where HEAD_ID='H1' and LINE_ID='3';
47 --update
48 update zhidao_20131014_tab2_1 set QTY=1000 where QTY=300;

 

转载于:https://www.cnblogs.com/zyx-/p/8043179.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值