ORA-04091错误的处理

来源:互联网 发布:网络变压器引脚定义 编辑:程序博客网 时间:2024/05/29 17:53

有要求如下,一个工作序列表,如果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;
/

测试通过。

 

 

原创粉丝点击