Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutating error on after insert trigger

The below code is giving a mutating error. Can any1 pls help in solving this.

    CREATE OR REPLACE TRIGGER aso_quote_cuhk_trigger
    BEFORE INSERT
    ON aso.aso_quote_headers_all
    FOR EACH ROW
    BEGIN
     UPDATE aso.aso_quote_headers_all
     SET quote_expiration_date=sysdate+90
     where quote_header_id=:new.quote_header_id;
    END;
    /
like image 945
user978417 Avatar asked Jun 22 '26 18:06

user978417


1 Answers

In oracle there are two levels of triggers: row level and table level.

Row level triggers are executed for each row. Table level triggers executed per statement, even if a statement changed more then one row.
In a row level trigger, you cannot select/update the table itself that has the trigger: you will get a mutating error.

In this case, there is no need for an UPDATE statement. Just try this:

CREATE OR REPLACE TRIGGER aso_quote_cuhk_trigger
BEFORE INSERT
ON aso.aso_quote_headers_all
FOR EACH ROW
BEGIN
 :new.quote_expiration_date=sysdate+90;     
END;
/

EDIT Rajesh mentioned it is possible, that before inserting a new row, OP wants to update all other records in the aso_quote_headers_all table.

Well, this is feasible, but it's a little tricky. To do this properly, you will need

  1. A pl/sql package and a variable in the package header that is modified by the triggers. This variable could be a list holding the IDs of newly inserted records. Row level after insert trigger would add a new ID to the list. The content of this package variable will be different for each different session, so let's call this variable session_variable.
  2. Row level after insert trigger, that would add new ID to the session_variable.
  3. Table level after insert trigger that would get IDs from the session_variable, process the ID and then remove it from the session_variable. This trigger could execute necessary selects/updates on the aso_quote_headers_all. After a newly inserted ID is processed, this trigger should make sure it gets removed from the session_variable.
like image 126
bpgergo Avatar answered Jun 24 '26 11:06

bpgergo