Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server triggers - order of execution

Does anyone know how SQL Server determines the order triggers (of same type, i.e. before triggers) are executed. And is there any way of changing this so that I can specify the order I want. If not, why not.

Thanks.

like image 739
HAdes Avatar asked Sep 18 '08 16:09

HAdes


People also ask

Which trigger is executed first?

The First and Last triggers must be two different triggers. First : Trigger is fired first. Last : Trigger is fired last.

In which event's trigger is executed?

Triggers (one or more) are implicitly fired (executed) by Oracle when a triggering event occurs, no matter which user is connected or which application is being used. Figure 20-1 shows a database application with some SQL statements that implicitly fire several triggers stored in the database.

When we have multiple triggers on the table for the same event in SQL how we can control the order of the execution?

SQL Server allows multiple triggers on the table for the same event and there is no defined order of execution of these triggers. We can set the order of a trigger to either first or last using procedure sp_settriggerorder. There can be only one first or last trigger for each statement on a table.

Which type of trigger must be created on order table such that when an order is placed the details should also get stored in Order Details table?

Database Triggers vs. Database triggers are defined on a table, stored in the associated database, and executed as a result of an INSERT, UPDATE, or DELETE statement being issued against a table, no matter which user or application issues the statement.


1 Answers

Using SetTriggerOrder is fine, but if your code depends on a specific sequence of execution, why not wrap all your triggers into stored procedures, and have the first one call the second, the second call the third, etc.

Then you simply have the first one execute in the trigger.

Someone in the future will be grateful that they didn't have to dig around in a system table to determine a custom execution sequence.

like image 52
JosephStyons Avatar answered Sep 21 '22 08:09

JosephStyons