A trigger body can include only SQL statements. To perform
actions or use logic that is not available in SQL statements, create
user-defined functions or stored procedures. Then invoke them from
within the trigger body.
About this task
Restriction: You cannot include
INSERT, UPDATE, DELETE, or MERGE statements in stored procedures or
user-defined functions that are invoked by a BEFORE TRIGGER. These
actions are not allowed, because BEFORE triggers must not modify any
table.
Procedure
To invoke a stored procedure or user-defined function
from a trigger:
-
Ensure that the stored procedure or user-defined function is
defined before the trigger is defined.
- Define procedures by using the CREATE PROCEDURE statement.
- Define triggers by using the CREATE FUNCTION statement.
- Invoke the user-defined function or stored procedure by
performing one of the following actions:
- To pass transition tables from the trigger to the user-defined
function or stored procedure, use table locators.
When
you call a user-defined function or stored procedure from a trigger,
you might want to give the function or procedure access to the entire
set of modified rows. In this case, use table locators to pass a pointer
to the old or new transition table.
Most of the code for using
a table locator is in the function or stored procedure that receives
the locator.
To pass the transition table from a trigger, specify
the parameter TABLE transition-table-name when
you invoke the function or stored procedure. This parameter causes Db2 to pass a table locator for
the transition table to the user-defined function or stored procedure.
For example, the following trigger passes a table locator for a transition
table NEWEMPS to stored procedure CHECKEMP:
CREATE TRIGGER EMPRAISE
AFTER UPDATE ON EMP
REFERENCING NEW TABLE AS NEWEMPS
FOR EACH STATEMENT MODE DB2SQL
BEGIN ATOMIC
CALL CHECKEMP(TABLE NEWEMPS);
END