MERGE requests can be used as triggering statements to fire triggers; however, they cannot be used as triggered statements.
When a MERGE request executes, triggers defined on UPDATE actions become activated if a WHEN MATCHED clause is specified, and triggers defined on INSERT actions become activated if a WHEN NOT MATCHED clause is specified.
The order of activation of UPDATE and INSERT triggers is the same as the order of WHEN MATCHED and WHEN NOT MATCHED clauses in the MERGE statement.
There is a slight deviation between the Vantage implementation of triggers with MERGE and the ANSI SQL:2011 standard for the implementation of triggers with MERGE. The ANSI SQL:2011 standard recommends that triggers be implemented in MERGE requests as follows.
First Clause Specified When Updates to Rows and Inserts of New Rows in the Target Table Occur |
Sequence of Actions During MERGE |
WHEN MATCHED (UPDATE specification) |
- All BEFORE triggers associated with UPDATE action are applied.
- The updates specified by the UPDATE specification are applied.
- Constraints are checked, which may result in executing referential actions.
- AFTER triggers associated with the UPDATE action are applied.
- BEFORE triggers associated with the INSERT action are applied.
- Inserts specified by the INSERT specification are applied.
- Constraints are checked, which might result in executing referential actions.
- AFTER triggers associated with the INSERT action are applied.
|
WHEN NOT MATCHED (INSERT specification) |
- BEFORE triggers associated with the INSERT action are applied.
- Inserts specified by the INSERT specification are applied.
- Constraints are checked, which may result in executing referential actions.
- AFTER triggers associated with the INSERT action are applied.
- All BEFORE triggers associated with the UPDATE action are applied.
- The updates specified by the UPDATE specification are applied.
- Constraints are checked, which might result in executing referential actions.
- AFTER triggers associated with the UPDATE action are applied.
|
In contrast, Vantage implements triggers in MERGE requests as follows:
First Clause Specified When Updates to Rows and Inserts of New Rows in the Target Table Occur |
Sequence of Actions During MERGE |
WHEN MATCHED (UPDATE specification) |
- All BEFORE triggers associated with UPDATE action are applied.
- All BEFORE triggers associated with INSERT action are applied.
- The updates specified by the UPDATE specification are applied.
- Constraints are checked, which might result in executing referential actions.
- Inserts specified by the INSERT specification are applied.
- Constraints are checked, which might result in executing referential actions.
- AFTER triggers associated with UPDATE action are applied.
- AFTER triggers associated with INSERT action are applied.
|
WHEN NOT MATCHED (INSERT specification) |
- All BEFORE triggers associated with INSERT action are applied.
- All BEFORE triggers associated with UPDATE action are applied.
- The updates specified by the UPDATE specification are applied.
- Constraints are checked, which might result in executing referential actions.
- Inserts specified by the INSERT specification are applied.
- Constraints are checked, which might result in executing referential actions.
- AFTER triggers associated with INSERT action are applied.
- AFTER triggers associated with UPDATE action are applied.
|
For information about the MERGE statement, see Teradata Vantage™ - SQL Data Manipulation Language, B035-1146.