2015-05-09 129 views
0

因此,我有一個任務,用於編寫觸發器,該觸發器應該在更新SALARY列的記錄中的兩列之間交換數據 - LAST_NAME和EMAIL。這就是我想出了SQL觸發錯誤報告

CREATE OR REPLACE TRIGGER SWAP 
    FOR UPDATE OF SALARY ON MY_EMP 
    COMPOUND TRIGGER 
    AFTER EACH ROW IS 
    BEGIN 
     UPDATE MY_EMP SET LAST_NAME = EMAIL, EMAIL = LAST_NAME WHERE :OLD.SALARY <> :NEW.SALARY; 
    END AFTER EACH ROW; 
END SWAP; 

,當我嘗試執行此:

UPDATE MY_EMP 
SET SALARY = SALARY + 100 
WHERE EMPLOYEE_ID = 198; 

我發現了一個錯誤報告:

UPDATE MY_EMP 
SET SALARY = SALARY + 100 
WHERE EMPLOYEE_ID = 198 
Error report - 
SQL Error: ORA-04091: table PWR_14_15_L_013209985.MY_EMP is mutating, trigger/function may not see it 
ORA-06512: at "PWR_14_15_L_013209985.SWAP", line 4 
ORA-04088: error during execution of trigger 'PWR_14_15_L_013209985.SWAP' 
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. 

我在做什麼錯這裏?任何解決方案

+1

錯誤消息清楚地解釋了問題所在。你還想要我們說什麼? – 2015-05-09 01:17:08

回答

0

更新前設置爲a,而不是修改基礎表,修改表的新值。

這將允許您在DML提交之前更新它們。