ORACLE新手,求助这个触发器怎么写?

benzhuer 2017-02-17 10:06:12
我用一个表TOLLLIST如下:
编码 名称 上级编码
01 政府性基金收入 #
0101 农网还贷资金收入 01
0102 地方农网还贷资金收入 01
02 专项收入 #
0201 排污费收入 01
0202 水资源费收入 01
我想写个触发器,当删除某个记录时自动根据上级编码列级联删除所有下级,触发器如下,这个触发器当删除某一行数据是没问题,但当用delete from TOLLLIST删除所有记录时就会报错00060 等待资源时检测到死锁,请问怎么改?
create or replace trigger TR_TOLLLIST
after delete on TOLLLIST
for each row
Pragma autonomous_transaction;
begin
delete TOLLLIST where superid = :old.tollid;
commit;
end TR_TOLLLIST;
...全文
266 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdsnhan 2017-02-20
  • 打赏
  • 举报
回复
为什么还要用事务自治? 举例分析这两条记录 01 政府性基金收入 # 0101 农网还贷资金收入 01 delete all的时候,第一个主事务发出指令,删除01,0101,锁定了两条记录 触发器中用的是事务自治,也就是说,与第一个主事务无关,删除0101时,处罚了第二个事务,删除01 然而此时,01这条记录被第一个主事务锁了。你等我,我等你,形成了死锁。 别用触发器了,4楼的方法很好,可控
POM_24 2017-02-17
  • 打赏
  • 举报
回复
触发器经常与死锁伴存啊,如果是BUSINIESS库最好不要使用; 1,两个表有主外键关联的话,可以使用级联删除;,2,可以试试写入存储过程内来删除;
benzhuer 2017-02-17
  • 打赏
  • 举报
回复
oracle 10g
卖水果的net 2017-02-17
  • 打赏
  • 举报
回复
楼主是哪个版本的库?
sxq129601 2017-02-17
  • 打赏
  • 举报
回复
Pragma autonomous_transaction 这个去除试试,能不能触发器最好别用
卖水果的net 2017-02-17
  • 打赏
  • 举报
回复

-- 不要用触发器了,用这种方法删除 


SQL> 
SQL> create table test(id int, pid int);
Table created
SQL> begin
  2      insert into test values(1,0);
  3      insert into test values(2,0);
  4      insert into test values(11,1);
  5      insert into test values(12,1);
  6      insert into test values(13,1);
  7      insert into test values(121,12);
  8      insert into test values(131,13);
  9      insert into test values(22,2);
 10      insert into test values(23,2);
 11      insert into test values(221,22);
 12      insert into test values(231,23);
 13  end;
 14  /
PL/SQL procedure successfully completed
SQL> delete test
  2  where id in (select id from test start with id = 1 connect by prior id = pid);
6 rows deleted
SQL> select * from test;
                                     ID                                     PID
--------------------------------------- ---------------------------------------
                                      2                                       0
                                     22                                       2
                                     23                                       2
                                    221                                      22
                                    231                                      23
SQL> drop table test purge;
Table dropped

SQL> 

17,140

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧