用于父子结构统计的触发器

情景:
一个存在父子关系的表结构 要求插入,更新时 父节点 的 统计出子节点对应字段的和
表结构:
create table TREE_PARENT_CHILD
(
id VARCHAR2(50) not null,
parentid VARCHAR2(50) not null,
need_sum NUMBER
);

触发器:
create or replace trigger trgger_parent_child_sum
  after insert  or  update on tree_parent_child  
  for each row
declare
  -- 自发事物
   PRAGMA AUTONOMOUS_TRANSACTION;
begin
  if inserting then
  dbms_output.put_line('--inserting---');
  update tree_parent_child t set t.need_sum = (select nvl(sum(need_sum),0)+:new.need_sum from   tree_parent_child where parentid= :new.parentid )
  where id= :new.parentid;
  commit;
  elsif updating then
    dbms_output.put_line('----updating--');
    update tree_parent_child t set t.need_sum = (select nvl(sum(need_sum),0)+:new.need_sum-:old.need_sum from   tree_parent_child where parentid= :new.parentid )
  where id= :new.parentid;
  commit;
  end if;
    
end trgger_parent_child_sum;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值