iSeries: Create a trigger

Date: 2016-03-19
CREATE OR REPLACE TRIGGER RowHistoryTrigger 
AFTER UPDATE OF TableWithRows ON MySchema
REFERENCING OLD AS old_row NEW AS new_row
FOR EACH ROW /*MODE DB2ROW | DB2SQL ? */
WHEN (old_row.Status <> new_row.Status
BEGIN
	DECLARE @GUID CHAR(36) NOT NULL DEFAULT '';
	SET @GUID=LOWER( 
		RIGHT(LPAD(HEX(INT(RAND(MICROSECOND(CURRENT_TIMESTAMP)) * 1000000000)), 8, '0'), 8) || '-' ||
		RIGHT(LPAD(HEX(INT(RAND() * 1000000000)), 8, '0'), 4) || '-' ||
		RIGHT(LPAD(HEX(INT(RAND() * 1000000000)), 8, '0'), 4) || '-' ||
		RIGHT(LPAD(HEX(INT(RAND() * 1000000000)), 8, '0'), 4) || '-' ||
		RIGHT(LPAD(HEX(INT(RAND() * 1000000000)), 8, '0'), 8) || RIGHT(LPAD(HEX(INT(RAND() * 1000000000)), 8, '0'), 4)
		);

	IF(NOT new_row.Status IS NULL) THEN
		INSERT INTO TableRowHistory (OID, INSERTEDAT, USERNAME, RECORDID, FROMSTATUS, TOSTATUS, MESSAGE) VALUES (@GUID, CURRENT_TIMESTAMP, USER, new_row.Oid, old_row.Status, new_row.Status, 'CHANGED');
	END IF
END

1390cookie-checkiSeries: Create a trigger