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的文章,解決問題。
原文如下:
思路就是在行級觸發器中,將當前需要刪除的行的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;
/
測試通過。