![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
CREATE TRIGGER ... {FOR | AFTER | INSTEAD OF} ... Do FOR and AFTER mean the same thing here? In particular, I am wondering whether a FOR trigger executes before or after the underlying data is modified. My text says before on one page and after on the next, and MSDN is not clear either. |
#3
| |||
| |||
|
|
Gene Wirchenko (genew (AT) ocis (DOT) net) writes: CREATE TRIGGER ... {FOR | AFTER | INSTEAD OF} ... Do FOR and AFTER mean the same thing here? In particular, I am wondering whether a FOR trigger executes before or after the underlying data is modified. My text says before on one page and after on the next, and MSDN is not clear either. FOR and AFTER are indeeed equivalent. Originally FOR was the only keyword. AFTER was added when they introduced INSTEAD OF. Notice that there are no BEFORE triggers in SQL Server, only INSTEAD OF. (In a real BEFORE trigger, you don't need to redo the command.) |
#4
| |||
| |||
|
|
Thank you. And what about when the underlying data gets modified? |
#5
| |||
| |||
|
|
Gene Wirchenko (genew (AT) ocis (DOT) net) writes: Thank you. And what about when the underlying data gets modified? An INSTEAD OF triggers fires instead of the INSERT/UPDATE/DELETE/MERGE statment that fired the trigger, and you need to resubmit the statement if you want to carry out the modification. FOR and AFTER triggers fires after the modification has taken place, and constraints have been validated. By default, if there is an error in the trigger, this rolls back the entire statement, even if there was no explicit transaction. |
#6
| |||
| |||
|
|
If I understand you correctly, then this covers my main concern: atomicity of the whole operation. |
#7
| |||
| |||
|
|
Gene Wirchenko (genew (AT) ocis (DOT) net) writes: If I understand you correctly, then this covers my main concern: atomicity of the whole operation. Yes, that is what you get. |
|
If you for some reason don't want the atomicity, there are ways to cheat. And, yes, I've seen people to ask for that. |
#8
| |||
| |||
|
|
Do they talk about their need to be flexible? Even brag about it a bit? |
![]() |
| Thread Tools | |
| Display Modes | |
| |