Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the value that fired the Oracle trigger

Am very new to Oracle triggers. Suppose I have a trigger on an insert in the table emp. Is there a way to find out what was the inserted record that fired the trigger. I wanted the trigger to have code that does something if the inserted record was a particular value.

like image 673
Eosphorus Avatar asked Jan 11 '12 21:01

Eosphorus


People also ask

How do you check a trigger is fired or not in Oracle?

A trigger will fire if it is enabled, and not fire if it is not. You can check the status from any of the xxx_TRIGGERS views.

What is outcome of trigger statement?

The CREATE TRIGGER statement is used to add triggers to the database schema. Triggers are database operations that are automatically performed when a specified database event occurs. Each trigger must specify that it will fire for one of the following operations: DELETE, INSERT, UPDATE.

How do I see triggers in a table in SQL Developer?

Input : SELECT TRIGGER_NAME FROM USER_TRIGGERS; Output : Input : SELECT * FROM USER_TRIGGERS; Output : NOTE: Using * means that we need all the attributes for that database object or Trigger to get displayed.

What is Raise_application_error in PL SQL?

The procedure RAISE_APPLICATION_ERROR lets you issue user-defined ORA- error messages from stored subprograms. That way, you can report errors to your application and avoid returning unhandled exceptions.


1 Answers

Assuming you have a row-level trigger, you can simply use the :NEW pseudo-record

CREATE TRIGGER name_of_trigger
  BEFORE INSERT ON emp
  FOR EACH ROW
DECLARE
  <<declare variables>>
BEGIN
  IF( :new.ename = 'JUSTIN' )
  THEN
    <<do something if the newly inserted ENAME value is 'JUSTIN'>>
  END IF;
END;

For a DDL trigger, the approach is completely different. In that case, the pseudofunctions ora_dict_obj_owner and ora_dict_obj_name will return the owner and name of the table that the DDL statement is operating on.

like image 159
Justin Cave Avatar answered Sep 19 '22 01:09

Justin Cave