joi, 27 februarie 2014

Capturing the updated column in a trigger

Sometimes, you need to make a difference between the fact that some column or another one is updated.
Thus, the trigger can fire in a very fine grained fashion.

The trick is very simple, just use the function updating(column_name).

e.g.:

create table test (field1 number, filed2 number);

CREATE OR REPLACE TRIGGER trigo
before update
  ON test 
 REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
declare 
----
BEGIN
if updating ('field1') and not updating ('field2') then
raise some_exception;
end if;
exception
WHEN some_exception THEN
    Raise_application_error (
      -20300,
      'bad ideea  !!!!' 
    );
END ;
/

In the example above, the trigger prevents updating of the column field1 and leaving the column field2 untouched.

Niciun comentariu:

Trimiteți un comentariu