How to create Triggers in SQL

How to write Triggers?

The following code shows the creation in DB2:

CREATE TRIGGER SALARY_UPDATE 
BEFORE UPDATE OF SALARAY ON EMP_TABLE
FOR EACH ROW MODE DB2SQL
WHEN (NEW.SALARY > (OLD.SALARY * 1.5))
BEGIN AUTOMIC
SIGNAL SQLSTATE '75001' ('RAISE EXCEEDS 50%');
END

SALARY_UPDATE:  Trigger Name

The trigger will fire before actual update. It applies to all update rows. Begin and End are the actual Trigger statements.

New value exceeds 50% of old salary. An error will raise. Triggers will be stored in Trigger package. Trigger packages also stored in SYSPACKAGE.

Advertisements

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.