用于父子结构统计的触发器
来源:互联网 发布:办公软件安装教程 编辑:程序博客网 时间:2024/05/21 05:57
情景:
一个存在父子关系的表结构 要求插入,更新时 父节点 的 统计出子节点对应字段的和
表结构:
create table TREE_PARENT_CHILD
(
id VARCHAR2(50) not null,
parentid VARCHAR2(50) not null,
need_sum NUMBER
);
(
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;
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;
- 用于父子结构统计的触发器
- 触发器有父子关系时的使用
- 在Oracle数据库中,用于数据备份的触发器及表结构
- 在Oracle数据库中,用于数据备份的触发器及表结构
- 触发器的基本结构
- ## 用于结构体的定义
- Javascript--父子数据层次结构和平行结构的转换
- PHP 创建用于统计的图形
- 用于统计函数执行时间的类
- 用于贝叶斯统计的R包
- 用于贝叶斯统计的R包
- UNITY 用于打点统计的小工具
- 一个用于跟踪和发现程序错误的触发器
- 一个用于跟踪和发现程序错误的触发器 --学习
- 用于 暂停(开启)所有触发器 的一个存储过程
- 利用自治事务技术写一个用于审计的触发器
- 在Salesforce中实现用于数据校验的触发器
- mysql 父子结构排序
- 黑马程序员 IO流的复习
- 网络常用命令
- 构造方法的重载的实例
- android环境搭建及项目创建
- 人民币转成大写
- 用于父子结构统计的触发器
- IT项目管理的六种错误思维
- 学习C语言,要从入门到精通,需要读哪些书
- 微软专业讲师教程(mcse 2003 server)下载(经典视频学习教材)e
- 严重: Exception sending context initialized event to listener instance of class org.springframework...
- Java 同步方式 (1) —— synchronized
- android项目目录结构
- 【JavaScript】KindEditor在线文本编辑器
- 参加工大毕业典礼后的一些感受