Structure of triggers
- Last Updated: March 30, 2020
- 2 minute read
- OpenEdge
- Version 12.2
- Documentation
Structure of triggers
Like a stored procedure, a trigger has a specification and a body.
The body of a trigger is the same as that of a stored procedure: BEGIN and END delimiters
enclosing a Java snippet. The Java code in the snippet defines the
triggered action that executes when the trigger is fired.
As with stored procedures, when it processes a CREATE TRIGGER statement, OpenEdge SQL adds
wrapper code to create a Java class and method that is invoked when
the trigger is fired.
The trigger specification, however, is different from a stored procedure specification. It contains the following elements:
- The
CREATEclause specifies the name of the trigger. OpenEdge SQL stores theCREATE TRIGGERstatement in the database undertrigname. It also usestrignamein the name of the Java class that OpenEdge SQL declares to wrap around the Java snippet. The class name uses the formatusername_trigname_TP, whereusernameis the user name of the database connection that issued theCREATE TRIGGERstatement. - The
BEFOREorAFTERkeywords specify the trigger action time: whether the triggered action implemented byjava_snippetexecutes before or after the triggeringINSERT,UPDATE, orDELETEstatement. - The
INSERT,DELETE, orUPDATEkeyword specifies which data modification command activates the trigger. IfUPDATEis the trigger event, this clause can include an optional column list. Updates to any of the specified columns will activate the trigger. (Updates to other columns in the table will not activate the trigger.) IfUPDATEis the triggering statement and does not include the optional column list, then theUPDATEstatement must specify all the table columns in order to activate the trigger. - The
ONtable_name clause specifies the table for which the specified trigger event activates the trigger. TheONclause cannot specify a view or a remote table. - The optional
REFERENCINGclause is allowed only if the trigger also specifies theFOR EACH ROWclause. It provides a mechanism for SQL to pass row values as input parameters to the stored procedure implemented byjava_snippet. The code in java_snippet uses thegetValuemethod of theNEWROWandOLDROWobjects to retrieve values of columns in rows affected by the trigger event and store them in procedure variables. - The
FOR EACHclause specifies the frequency with which the triggered action implemented byjava_snippetexecutes. -
FOR EACH ROWmeans the triggered action executes once for each row being updated by the triggering statement.CREATE TRIGGERmust include theFOR EACH ROWclause if it also includes aREFERENCINGclause. -
FOR EACH STATEMENTmeans the triggered action executes only once for the whole triggering statement.FOR EACH STATEMENTis the default. - The
IMPORTclause is the same as in stored procedures. It specifies standard Java classes to import.
The following example shows the elements of a trigger.
|