ORA-04091错误的处理

有要求如下,一个工作序列表,如果flag字段被更新为OK,则将这条记录转移到log表中。

直接在触发器中,进行自身删除,报错:

 

ORA-04091: table AAA.AAAAAA is mutating, trigger/function may not see it
ORA-06512: at "AAA.TR_AAAAAA", line 4
ORA-04088: error during execution of trigger AAA.TR_AAAAAA'

 

后来参考了ASKTOM的文章,解决问题。

原文如下:

http://asktom.oracle.com/tkyte/Mutate/

 

思路就是在行级触发器中,将当前需要删除的行的rowid记录到pl/sql包的变量中,

然后在语句级的触发器中,将变量指向的行删除。

 

测试用的包和触发器如下: (共需两个,一个row level, 一个statement level)

 

create or replace package test_pkg is

  type rowidArray is table of rowid index by binary_integer;
  mRows rowidArray;
  emtpy rowidArray;

end test_pkg;
/

 

create or replace trigger tr_aaaaaa
  after update on aaaaaa 
  for each row
begin

  if ('OK' = :new.flag) then
    insert into aaaaaa_his(, , ,)
    values(:new.,:new.,:new.,:new.);
 
    test_pkg.mRows(test_pkg.mRows.count + 1) := :new.rowid;
  end if;

end tr_aaaaaa;
/

 

create or replace trigger tr_aaaaaa_his
  after insert on aaaaaa_his 
begin
  for i in 1 .. test_pkg.mRows.count loop
    delete aaaaaa where rowid = test_pkg.mRows(i);
 
  end loop;
 
  test_pkg.mRows := test_pkg.emtpy;
 
 
end tr_aaaaaa_his;
/

测试通过。

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值