SQL Server 2000 Trigger Enhancements
November 1, 2000
SQL Server 2000 greatly enhances trigger functionality, extending the capabilities of the triggers you already know and love, and adding a whole new type of trigger, the "Instead Of" trigger.
If you are already familiar with programming triggers then read on, but if you are new to the subject then it would be worth reading about triggers in Books Online, or taking a look at the Triggers section on my own SQL Server page, before reading this article.
After Triggers are the type of trigger that existed prior to SQL Server 2000, the "After" name is new and helps differentiate between this type of trigger and the new "Instead Of" trigger type, which will be covered later.
Multiple After Triggers
More than one trigger can now be defined on a table for each Insert/Update/Delete. Although in general you might not want to do this (it's easy to get confused if you over-use triggers) there are situations where this is ideal. One example that springs to mind is that you can split your triggers up into two categories:
This would allow you to alter triggers of one type without fear of accidentally breaking the other.
If you are using multiple triggers, it is of course essential to know which order they fire in. A new stored procedure called sp_settriggerorder allows you to set a trigger to be either the "first" or "last" to fire.
If you want more than two triggers to fire in a specific order, there is no way to specifically define this. A deeply unscientific test I did indicated that multiple triggers for the same table and operation will run in the order they were created unless you specifically tell them otherwise. I would not recommend relying on this though.
Instead Of Triggers
Instead Of Triggers fire instead of the operation that fires the trigger, so if you define an Instead Of trigger on a table for the Delete operation, they try to delete rows, they will not actually get deleted (unless you issue another delete instruction from within the trigger) as in this simple example:
If you were to print out the contents of the Inserted and Deleted tables from inside an Instead Of trigger you would see they behave in exactly the same way as normal. In this case the Deleted table holds the rows you were trying to delete, even though they will not get deleted.
Instead of Triggers can be used in some very powerful ways!
Mixing Trigger Types
If you were to define an Instead Of trigger and an After trigger on the same table for the same operation, what would happen?
Because an After trigger fires after an operation completes, and an 'instead of' trigger prevents the operation from taking place, the After trigger would never fire in this situation.
However, if an Instead Of trigger on a (say) delete operation contains a subsequent delete on the same table, then any After trigger defined for the delete operation on that table will fire on the basis of the delete statement issued from the Instead Of trigger. The original delete statement is not executed, only the Delete in the Instead Of trigger runs.
This code sample creates a trigger of each type, and changed the nature of the delete statement issued so that only comics that have a value of 0 in the preserve column can be deleted.
For more details on Instead Of triggers take a look at this article, and of course Books Online.