CREATE TRIGGER
- Last Updated: March 30, 2020
- 4 minute read
- OpenEdge
- Version 12.2
- Documentation
Creates a trigger for the specified table. A trigger is a special type of automatically executed stored procedure that helps ensure referential integrity for a database.
Triggers
contain Java source code that can use SQL Java classes to carry
out database operations. Triggers are automatically activated when
an INSERT, UPDATE, or DELETE statement
changes the trigger's target table. The Java source code details
what actions the trigger takes when it is activated.
Syntax
|
Parameters
- owner_name
-
Specifies the owner of the trigger. If the name is different from the user name of the user executing the statement, then the user must have
DBAprivileges. - trigname
-
Names the trigger.
DROP TRIGGERstatements specify the trigger name defined here. SQL also uses trigname in the name of the Java class that it creates from the Java snippet. - BEFORE | AFTER
-
Denotes the trigger action time. The trigger action time specifies whether the triggered action, implemented by java_snippet, executes
BEFOREorAFTERthe invokingINSERT,UPDATE, orDELETEstatement. - INSERT |DELETE |UPDATE [ OF column_name[ , ...]]
-
Denotes the trigger event. The trigger event is the statement that activates the trigger.
If
UPDATEis the triggering statement, this clause can include an optional column list. Only updates to any of the specified columns will activate the trigger. IfUPDATEis the triggering statement and does not include the optional column list, then anyUPDATEon the table will activate the trigger. - ON table_name
-
Identifies the name of the table where the trigger is defined. A triggering statement that specifies table_name causes the trigger to execute. table_name cannot be the name of a view.
- REFERENCING OLDROW [ , NEWROW ]| NEWROW [ , OLDROW ]
-
Provides a mechanism for SQL to pass row values as input parameters to the stored procedure implemented by
java_snippet. The code injava_snippetuses thegetValuemethod of theNEWROWandOLDROWobjects to retrieve values of columns in rows affected by the trigger event and store them in procedure variables. This clause is allowed only if the trigger specifies theFOR EACH ROWclause.The meaning of the
OLDROWandNEWROWarguments of theREFERENCINGclause depends on whether the trigger event isINSERT, UPDATE,orDELETE. For example:-
INSERT...REFERENCING NEWROWmeans the triggered action can access values of columns of each row inserted. SQL passes the column values specified by theINSERTstatement. -
INSERT...REFERENCING OLDROWis meaningless, since there are no existing values for a row being inserted.INSERT...REFERENCING OLDROWgenerates a syntax error. -
UPDATE...REFERENCING OLDROWmeans the triggered action can access the values of columns, before they are changed, of each row updated. SQL passes the column values of the row as it exists in the database before the update operation. -
DELETE...REFERENCING OLDROWmeans the triggered action can access values of columns of each row deleted. SQL passes the column values of the row as it exists in the database before the delete operation. -
DELETE...REFERENCING NEWROWis meaningless, since there are no new existing values to pass for a row being deleted.DELETE...REFERENCING OLDROWgenerates a syntax error. -
UPDATEis the only triggering statement that allows bothNEWROWandOLDROWin theREFERENCINGclause. -
UPDATE...REFERENCING NEWROWmeans the triggered action can access the values of columns, after they are changed, of each row updated. SQL passes the column values specified by theUPDATEstatement. - The trigger action time (
BEFOREorAFTER) does not affect the meaning of theREFERENCINGclause. For instance,BEFORE UPDATE...REFERENCING NEWROWstill means the values of columns after they are updated will be available to the triggered action. - The
REFERENCINGclause generates an error if the trigger does not include theFOR EACH ROWclause.
-
- FOR EACH { ROW | STATEMENT }
-
Controls the execution frequency of the triggered action implemented by java_snippet.
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. - IMPORT java_import_clause
-
Specifies standard Java classes to import. The
IMPORTkeyword must be uppercase and on a separate line. - BEGIN
-
java_snippet
- END
-
Denotes the body of the trigger or the triggered action. The body contains the Java source code that implements the actions to be completed when a triggering statement specifies the target table. The Java statements become a method in a class that SQL creates and submits to the Java compiler.
The
BEGINandENDkeywords must be uppercase and on separate lines.
Notes
- Triggers can take action on their own table so that they invoke themselves. SQL limits such recursion to five levels.
- You can you have multiple triggers on the same table. Multiple
UPDATEtriggers on the same table must specify different columns. SQL executes all triggers applicable to a given combination of table, trigger event, and action time. - The actions carried out by a trigger can fire another trigger. When this happens, the other trigger's actions execute before the rest of the first trigger finishes executing.
- If a constraint and trigger are both invoked by a particular SQL statement, SQL checks constraints first, so any data modification that violates a constraint does not also fire a trigger.
- To modify an existing trigger, you must delete it and issue
another
CREATE TRIGGERstatement. You can query the systrigger system table for information about the trigger before you delete it. - The code in java_snippet uses the
getValuemethod of theNEWROWandOLDROWobjects. ThegetValuemethod is valid onOLDROWbefore or after an update or delete andNEWROWbefore or after an update or insert; thesetValuemethod is only valid onNEWROWbefore an insert or update.
Examples
CREATE TRIGGER statement
The following code segment illustrates how to use the CREATE TRIGGER
statement:
|
CREATE TRIGGER statement
The
following code segment illustrates how to set values for a new row
in the CREATE TRIGGER statement:
|
For more information on creating and using triggers, see Develop SQL for OpenEdge.