Friday, December 21, 2018

Oracle triggers

Oracle trigger can be used to modify data before update or insert in the same table.

Oracle update trigger:

CREATE OR REPLACE TRIGGER update_time_col
  BEFORE
    UPDATE OF col_1, col_2
  ON tab
  FOR EACH ROW
BEGIN
  CASE
    WHEN UPDATING('col_1') THEN
      :new.time_col := SYSTIMESTAMP - INTERVAL '1' DAY;
      :new.col_1 := 'updated ' || :old.col_1;
    WHEN UPDATING('col_2') THEN
      :new.time_col := SYSTIMESTAMP + INTERVAL '1' DAY;
      :new.col_2 := 'updated ' || :old.col_2;    
  END CASE;
END;
/

Oracle insert trigger:

CREATE OR REPLACE TRIGGER insert_time_col
  BEFORE
    INSERT
  ON tab
  FOR EACH ROW
BEGIN
  :new.time_col := SYSTIMESTAMP + INTERVAL '10' DAY;
  :new.col_1 := 'insert ' || :new.col_1;  
  :new.col_2 := 'insert ' || :new.col_2;
END;
/




No comments:

Post a Comment