可以說我有作爲follows--觸發不能讀取表,由同一臺被解僱後
create table employees
(
eno number(4) not null primary key,
ename varchar2(30),
zip number(5) references zipcodes,
hdate date
);
我用下面的代碼塊
create or replace TRIGGER COPY_LAST_ONO
AFTER INSERT ON ORDERS
FOR EACH ROW
DECLARE
ID_FROM_ORDER_TABLE VARCHAR2(10);
BEGIN
SELECT MAX(ORDERS.ONO)INTO ID_FROM_ORDER_TABLE from ORDERS ;
DBMS_OUTPUT.PUT_LINE(ID_FROM_ORDER_TABLE);
INSERT INTO BACKUP_ONO VALUES(VALUE1, VALUE2,VALUE3, ID_FROM_ORDER_TABLE);
END;
創建觸發器的表
觸發器在插入後觸發,並嘗試從表中讀取(邏輯上duhh!),但是oracle給了我一個錯誤並要求我修改觸發器,以便它不讀取表。錯誤代碼 -
Error report -
SQL Error: ORA-04091: table TEST1.ORDERS is mutating, trigger/function may not see it
ORA-06512: at "TEST1.COPY_LAST_ONO", line 8
ORA-04088: error during execution of trigger 'TEST1.LOG_INSERT'
04091. 00000 - "table %s.%s is mutating, trigger/function may not see it"
*Cause: A trigger (or a user defined plsql function that is referenced in
this statement) attempted to look at (or modify) a table that was
in the middle of being modified by the statement which fired it.
*Action: Rewrite the trigger (or function) so it does not read that table.
我試圖實現與該觸發器是被INSERTED
後複製的最後INSERTED
ONO
(這對於ORDER
表的主鍵)立即向不同的表。我不明白的是,爲什麼oracle抱怨?觸發器試圖讀取AFTER
的插入!
想法?解?
千恩萬謝
是否'BACKUP_ONO'表有一個'外國key'到餐桌'ORDERS'在其上觸發執行?這可能會導致這樣的事情發生。你可以閱讀更多http://www.dba-oracle.com/t_avoiding_mutating_table_error.htm – InSane
@InSane沒有那樣的情況。 'BACKUP_ONO'只有一個主鍵。但是有一個屬性名爲'ONO',因爲我將它從'ORDERS'複製到'BACKUP_ONO' – envyM6
也許與您的錯誤無關 - 但是使用ONO不是更好:NEW而不是嘗試通過MAX獲取?您的訂單中可能已插入ONO? – InSane