问题
I'm trying to do a trigger but I get a mutating table error. The SQL code is something like this:
CREATE OR REPLACE TRIGGER CHK_Apartado_D
BEFORE INSERT OR UPDATE ON CONTRACTS
FOR EACH ROW
DECLARE
errorvisualizacion EXCEPTION;
local_enddate DATE;
BEGIN
SELECT enddate INTO local_enddate FROM CONTRACTS WHERE clientid=:new.clientid;
IF local_enddate > SYSDATE OR local_enddate IS NULL
THEN
UPDATE CONTRACTS SET enddate = SYSDATE - 1 WHERE clientid=:new.clientid;
END IF;
END CHK_Apartado_B;
/
And the error that I get is this:
Informe de error -
Error SQL: ORA-04091: table HR.CONTRACTS is mutating, trigger/function may not see it
ORA-06512: at "HR.CHK_APARTADO_D", line 5
ORA-04088: error during execution of trigger 'HR.CHK_APARTADO_D'
ORA-06512: at "HR.CHK_APARTADO_D", line 8
ORA-04088: error during execution of trigger 'HR.CHK_APARTADO_D'
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.
When I INSERT a new contract I have to check if that client have an other contract in actual date, and if he have I must update de end date contracte to yesterday and let the new INSERT. So, how can I do to prevent the mutating table?
回答1:
Your trigger fires on update or insert of CONTRACTS and then tries to update CONTRACTS, which fires the trigger.... See the problem?
You need to work out the enddate and then actually do the insert / update.
回答2:
A common solution is Using Compound Triggers to Avoid Mutating-Table Error.
Better still, don't attempt to use triggers to implement complex business logic. Instead, build an API package with a procedure insert_contract
that implements the business rules, and ensure (via privileges) that users call that API rather than inserting directly into the table. Triggers can get very messy, very quickly.
来源:https://stackoverflow.com/questions/43521031/mutating-table-on-oracle-sql-trigger