Multiple events supported in a single SQL trigger
Native triggers already have the ability to handle INSERT, UPDATE and DELETE triggering events within a single program. By allowing SQL trigger programs to handle multiple events, the management, installation, and maintenance are improved.
A multiple event trigger is a trigger that can handle INSERT, UPDATE, and DELETE triggering events within a single SQL trigger program. The ability to handle more than one event in a single program simplifies management of triggers.
In the body of the trigger, the new INSERTING, UPDATING, and DELETING predicates can be used to distinguish between the events that cause the trigger to fire. These predicates can be specified in control statements (like IF) or within any SQL statement that accepts a predicate (like SELECT or UPDATE).
Example...the following trigger:
- Increments the number of employees each time a new person is hired
- Decrements the number of employees each time an employee leaves the company
- Raises an error when a salary increase is greater than ten percent
REFERENCING NEW AS N OLD AS O FOR EACH ROW
THEN UPDATE COMPANY_STATS SET NBREMP = NBREMP + 1;
THEN UPDATE COMPANY_STATS SET NBREMP = NBREMP - 1;
IF UPDATING AND (N.SALARY > 1.1 * O.SALARY)
THEN SIGNAL SQLSTATE '75000'
SET MESSAGE_TEXT = 'Salary increase > 10%'
Consult the IBM i Knowledge Center for more details:
14 January 2020