Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORACLE and TRIGGERS (inserted, updated, deleted)

I would like to use a trigger on a table which will be fired every time a row is inserted, updated, or deleted.

I wrote something like this:

CREATE or REPLACE TRIGGER test001
  AFTER INSERT OR DELETE OR UPDATE ON tabletest001
  REFERENCING OLD AS old_buffer NEW AS new_buffer 
  FOR EACH ROW WHEN (new_buffer.field1 = 'HBP00') 

and it works. Since I would like to do the same things if the row is inserted, updated, or deleted, I would like to know what's happening in the trigger. I think I can manage to find if the row is inserted or updated (I can check the old_buffer with the new_buffer). How can I know if the row has been deleted?

like image 341
LeftyX Avatar asked Sep 06 '25 12:09

LeftyX


1 Answers

From Using Triggers:

Detecting the DML Operation That Fired a Trigger

If more than one type of DML operation can fire a trigger (for example, ON INSERT OR DELETE OR UPDATE OF Emp_tab), the trigger body can use the conditional predicates INSERTING, DELETING, and UPDATING to check which type of statement fire the trigger.

So

IF DELETING THEN ... END IF;

should work for your case.

like image 122
devio Avatar answered Sep 09 '25 02:09

devio