永发信息网

如何在触发器里写:当一张表被更新时,更新它关联的一张表

答案:1  悬赏:10  手机版
解决时间 2021-03-29 07:27
如何在触发器里写:当一张表被更新时,更新它关联的一张表
最佳答案
哎。。。刚开完会,不然早就弄好了,给你加了会儿班,呵呵。
环境—oracle
最不理想的就是行级触发器里面不用使用select原表等操作,而表级触发器又不能用:new和:old,
所以只能这样判断update、insert、delete来实现,否则得使用行级和表级和中间表才能实现,比较麻烦了。
--建表:
create table system.zhidao_20131014_tab2_1
(
       HEAD_ID  varchar2(10),
       LINE_ID  varchar2(10),
       QTY  number
);
create table system.zhidao_20131014_tab2_2
(
       HEAD_ID  varchar2(10),
       SUMQTY  number
);
--造数:
insert into system.zhidao_20131014_tab2_1
select 'H1','1',100 from dual
union all
select 'H1','2',200 from dual
union all
select 'H1','2',300 from dual
union all
select 'H1','2',100 from dual;

insert into system.zhidao_20131014_tab2_2
select 'H1',700 from dual;
commit;
--触发器:
create or replace trigger tr_zhidao
after insert or update or delete
on system.zhidao_20131014_tab2_1
for each row
begin
  case
    when updating then
      update system.zhidao_20131014_tab2_2 t set SUMQTY=SUMQTY - :old.QTY + :new.QTY where t.head_id=:new.head_id;
    when inserting then
      update system.zhidao_20131014_tab2_2 t set SUMQTY=SUMQTY + :new.QTY where t.head_id=:new.head_id;
    when deleting then
      update system.zhidao_20131014_tab2_2 t set SUMQTY=SUMQTY - :old.QTY where t.head_id=:old.head_id;
  end case;
end;
--测试:
--insert
insert into system.zhidao_20131014_tab2_1 
select 'H1','3',300 from dual;
--delete
delete system.zhidao_20131014_tab2_1 where HEAD_ID='H1' and LINE_ID='3';
--update
update system.zhidao_20131014_tab2_1 set QTY=1000 where QTY=300;有问题再追问,望采纳啊。
追问不好意思,是SQL SERVER,忘了说了追答create trigger tr_zhidao
on system.zhidao_20131014_tab2_1
for insert,update,delete
as
declare @oldQTY number,@newQTY number,@oldHID varchar2(10),@newHID varchar2(10);
begin
  if ((exists (select 1 from inserted)) and (exists (select 1 from deleted)))
    begin
      select @newQTY=QTY,@newHID=head_id from inserted; 
      select @oldQTY=QTY from deleted;
      update system.zhidao_20131014_tab2_2 t set SUMQTY=SUMQTY - @oldQTY + @newQTY where t.head_id=@newHID;
    end
  else if (exists (select 1 from inserted) and not exists (select 1 from deleted))
    begin
      select @newQTY=QTY,@newHID=head_id from inserted; 
      update system.zhidao_20131014_tab2_2 t set SUMQTY=SUMQTY + @newQTY where t.head_id=@newHID;
    end
  else if (not exists (select 1 from inserted) and exists (select 1 from deleted))
    begin
      select @oldQTY=QTY,@oldHID=head_id from deleted;
      update system.zhidao_20131014_tab2_2 t set SUMQTY=SUMQTY - @oldQTY where t.head_id=@oldHID;
    end
end
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
重生之官场风流 正文 第三五二章 什么人啊,t
cvc面料和俊织面料哪个好,我在买衣服遇到两
上卦5下卦3 动爻数为6 梅花易数求健康吉还是
宝宝白球比偏高的原因
灸法中的换一桩是什么意思
如何分辨GTR GTS GTT类型及32 33 34等型号?
为什么被她放弃,被人笑痴,而我怎么能相信这
凌动D2500,D2700,D2800性能怎么样!相当于
guccl太阳镜那个国家买最便宜
黑棍硬钓鱼竿6.3米7.1米8.3米多少钱
yy4.17用不了怎么办,统一登陆就说版本低?
马里拉岛为什么叫吕宋岛?
知聊软件被禁止登录怎么解除
单选题不能肯定两个平面一定垂直的情况是A.两
Windows如何批量更名
推荐资讯
心中想2位以上的数字,然后减去你想的数字加
我该死心吗
WiiU的Wii破解后,能认什么硬盘??
如何选购靠谱的金骏眉
作文记住那一瞬间六百字
篮球故事丨命运让库里经历了什么造就了他的巨
我第一次交女票,应该怎么接吻?让她不觉得我
php如何通过url参数进行查询?
数名侦探柯南白马探出现的集数
做糖耐先把葡萄糖喝了没加水再喝水会不会有差
严重脱发一个半月有余,控制不住该怎么办
现在还有人用塞班系统的手机吗
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?