Saturday, 28 May 2011

DB2: Create a trigger to update a timestamp column

Question: How to create a trigger that will update a timestamp column with every change made to other columns content ?

Answer:
Given table “DEMO.SHIPMENTS” that contains timestamp column “LAST_UPDATE_TS”, the SQL statement shown below will create a trigger that updates the column content to current timestamp value (when ever an update statement for any column is executed on the table).

CREATE TRIGGER DEMO.ONUPDATE NO CASCADE BEFORE UPDATE ON DEMO.SHIPMENTS REFERENCING OLD AS old NEW AS new FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
set new."LAST_UPDATE_TS"=current timestamp;
END

No comments:

Post a Comment