News
Abstract
Multiple events supported in a single SQL trigger
Content
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
BEGIN
IF INSERTING
THEN UPDATE COMPANY_STATS SET NBREMP = NBREMP + 1;
END IF;
IF DELETING
THEN UPDATE COMPANY_STATS SET NBREMP = NBREMP - 1;
END IF;
IF UPDATING AND (N.SALARY > 1.1 * O.SALARY)
THEN SIGNAL SQLSTATE '75000'
SET MESSAGE_TEXT = 'Salary increase > 10%'
END IF;
END
Consult the IBM i Knowledge Center for more details:
Was this topic helpful?
Document Information
Modified date:
14 January 2020
UID
ibm11167316