Skip to Main Content
  • Questions
  • Update query in after insert trigger

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Hima.

Asked: November 16, 2016 - 7:04 pm UTC

Last updated: January 30, 2018 - 2:45 am UTC

Version: 11.1.0.6.0

Viewed 10K+ times! This question is

You Asked

Hello Tom-
my requirement is When a record is inserted into ERR table, a post-event trigger will fire updating the D field to accept sys date (3 columns has to update in my original requirement).
I've written the below code, however after the record is inserted it's not being updated with sys date in D rather I got the mutating error. Thank you..!!

SQL> CREATE OR REPLACE TRIGGER trig_2
2 after insert ON ERR
3 for each row
4 begin
5 if inserting then
6 update err set d=sysdate where x = :new.x;
7 commit;
8 end if;
9 end;
10 /

Trigger created.

SQL> select * from err;

D X
--------- ----------
16-NOV-16 1

SQL> insert into err(x) values(2);
insert into err(x) values(2)
*
ERROR at line 1:
ORA-04091: table ERR is mutating, trigger/function may not see it
ORA-06512: at "TRIG_2", line 3
ORA-04088: error during execution of trigger 'TRIG_2'


SQL> select * from err;

D X
--------- ----------
16-NOV-16 1

SQL>

and Connor said...

Just change it to a before-insert trigger, and you dont need SQL at all. It will just be:

...
for each row
begin
  :new.d := sysdate;
end;


Of course, it might be even better just to define the column as DEFAULT sysdate.

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Update in Before Insert Trigger

Hima, November 17, 2016 - 8:52 pm UTC

thank you very much for your help and suggestions.

I come across new scenario during update trigger code testing.
question:
1. During insert operation on T1, it goes inside if inserting block.
2. during update operation on T1, it goes inside if updating block. But, when it hits else blocks it runs insert statements which are supposed to run. But, also it is calling BEFORE INSERT trigger which has been defined at #1 on T1.

My requirement is, it should not call the other INSERT trigger (#1) when it is being called by UPDATE TRIGGER and then follows by INSERT in else blocks. Please look into below pseudo code.

Expected Result: When I do update on T1, based on conditions it has to do inserts in else blocks only but not the insert statement of #1.

create Trigger Trg
before insert or update of c1,c2 on T1
for each row
begin
if inserting then
:new.c1 := 'A';
:new.c2 := 'B';
end if;
if updating then
if :old.active = 'INACTIVE' then
raise_application_error(-20000,'do not update');
else if :old.active = 'FALSE' then
insert query on T1(c1,c2) values(:new.c1,:new.c2);
else
insert query on T1(c1,c2,active) values(:old.c1,:old.c2,'FALSE');
end if;
end if;
end if;

end;
/

A reader, November 18, 2016 - 3:59 pm UTC

Thank you, it works perfectly.
Connor McDonald
November 19, 2016 - 1:59 am UTC

glad we could help

Update same table with previous date data

Srinivas, January 29, 2018 - 6:53 pm UTC

Hi All,
I have write update code using merge and I have written Insert code as well.
My requirement is when effective_date = current_date it should update and if it is not equal then it should insert.
It is working but while inserting if we modify any data then it is not reflecting.
Connor McDonald
January 30, 2018 - 2:45 am UTC

Please ask this as a new question with a *full* test case.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library