Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When implementing a statement-level trigger on a table, is it possible to obtain the OLD and NEW records for all affected rows?

In Oracle, you can write a row-level trigger by specifying the FOR EACH ROW clause in the CREATE TRIGGER statement:

CREATE TRIGGER MY_FANCY_TRIGGER
  BEFORE UPDATE ON MY_TABLE
  FOR EACH ROW
BEGIN
  IF :OLD.my_id_column > 4 AND :NEW.some_other_column <> 'foo' THEN
    -- ...
  END IF;
END;

Such a trigger allows you to view the before and after versions of each affected row (:OLD and :NEW respectively). For example, the following statement will cause this trigger to execute once for each row in MY_TABLE:

UPDATE MY_TABLE SET some_other_column = 'bar';

By eliminating the FOR EACH ROW clause, the trigger becomes a statement-level trigger. This means that it only be executed once per statement, regardless of how many rows (if any) were affected by the statement. Unfortunately, statement-level triggers do not have the :OLD and :NEW variables available (because the number of affected rows many vary).

Is it possible to obtain the :OLD and :NEW values for all affected rows inside a statement-level trigger? I have some processing that I would prefer to only occur once per statement.

like image 235
Adam Paynter Avatar asked May 05 '11 23:05

Adam Paynter


People also ask

Is it possible to create the following trigger before or after update trigger for each row?

Old and new values are available in both BEFORE and AFTER row triggers. A new column value can be assigned in a BEFORE row trigger, but not in an AFTER row trigger (because the triggering statement takes effect before an AFTER row trigger is fired).

What is difference between row level trigger and statement level trigger?

Row level triggers executes once for each and every row in the transaction. Statement level triggers executes only once for each single transaction. Specifically used for data auditing purpose. Used for enforcing all additional security on the transactions performed on the table.

How will you identify that the trigger is a row level trigger?

A row-level trigger fires once for each row that is affected by a triggering event. For example, if deletion is defined as a triggering event for a particular table, and a single DELETE statement deletes five rows from that table, the trigger fires five times, once for each row.

What is row trigger and statement trigger?

A statement trigger fires once per triggering event and regardless of whether any rows are modified by the insert, update, or delete event. row triggers. A row trigger fires once for each row affected by the triggering event. If no rows are affected, the trigger does not fire.


1 Answers

One approach is the one suggested by Justin Cave to store away information in row level triggers in a separate package collections.

If you are using 11g then the right approach will be to use Compound triggers. This avoids the creation of separate package to hold the keys collection- it can be done in the trigger itself,

like image 138
josephj1989 Avatar answered Oct 17 '22 20:10

josephj1989