Let\'s say I have table TabA with columns:
col1 - primary key (but not identity)
col2 - foreign key
col3 -
This is easy to test by setting up test tables as described with print statements in the triggers and simply trying to insert invalid values.
Of course, I did it! And agree with you - get the same result from 1 to 8. And what is embarrass for me is the quote from "Microsoft SQL Server 2008 Bible" book (by Paul Nielsen). That is (on page 637):
Every transaction moves through the various checks and code in the following order:
- IDENTITY INSERT check
- Nullability constraint
- Data-type check
- INSTEAD OF trigger execution. If an INSTEAD OF trigger exists, then execution of the DML stops here. INSTEAD OF triggers are not recursive. Therefore, if the INSERT trigger executes another DML command, then the INSTEAD OF trigger will be ignored the second time around (recursive triggers are covered later in this chapter).
- Primary-key constraint
- Check constraints
- Foreign-key constraint
- DML execution and update to the transaction log
- AFTER trigger execution
- Commit transaction
So, as you see, this IT-Pro disagree with you and me. He, for example, give INSTEAD OF trigger number 4 whereas we give to it number 1. This quote just baffle me!
This is easy to test by setting up test tables as described with print statements in the triggers and simply trying to insert invalid values. Doing so gave for me
As far as I know the order of 1,7, and 8 are guaranteed. The rest are arbitrary. Any error will stop succeeding steps.
General order of execution for INSERT, UPDATE, DELETE statements:
sp_settriggerorder
, execute those at the appropriate point, with any remaining triggers executed in undefined order.You may have only 1 INSTEAD OF trigger (per action: INSERT, UPDATE or DELETE). That trigger will always get executed before any AFTER triggers, since it executes in lieue of the corresponding INSERT, UPDATE or DELETE statement.
AFTER triggers are always executed, oddly enough, AFTER the data modification statement executes.
NOTE: If you have an INSTEAD OF trigger, it is unclear to me, not having spent any real amount of time fussing with INSTEAD OF triggers, whether or not table/row constraints are enforced prior to execution of the INSTEAD OF trigger. That's something you might want to experiment with.