views:

1733

answers:

8

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

Thanks.

A: 

The order is set by sql server, the only thing you can do is use a system sp (sp_settriggerorder) to set which trigger will fire first and which will fire last.

Beyond setting the first and last triggers to fire, you can't modify or tell which order sql server will use. Therefore you will want to build your triggers so they do not rely on which order they are fired. Even if you determine the order they fire in today, it may change tomorrow.

This information is based on Sql Server 2000, however I do not believe 2005/2008 act differently in this regard.

Dave_H
+3  A: 

Use sp_Settriggerorder stored procedure, you can define the execution order of the trigger.

sp_settriggerorder [ @triggername = ] ‘[ triggerschema. ] triggername’ , [ @order = ] ‘value’ , [ @stmttype = ] ’statement_type’ [ , [ @namespace = ] { ‘DATABASE’ | ‘SERVER’ | NULL } ]

The second parameter, “order” can take three values which means that it can take into account up-to three triggers.

First – Trigger is fired first Last - Trigger is fired last None – Trigger is fired in random order.

EvilEddie
A: 

Use this system stored procedure:

sp_settriggerorder[@triggername = ] 'triggername', [@order = ] 'value', [@stmttype = ] 'statement_type'
Ben Hoffstein
+2  A: 

sp_settriggerorder only applies to AFTER triggers.

Luke Bennett
+2  A: 

You can guarantee which trigger is fired first, which trigger is fired last and which ones fire in the middle by using sp_settriggerorder. If you need to synchronize more than three it does not appear possible in SQL Server 2005.

Here is a sample taken from here (The linked article has much more information).

sp_settriggerorder [ @triggername = ] ‘[ triggerschema. ] triggername’
, [ @order = ] ‘value’
, [ @stmttype = ] ’statement_type’
[ , [ @namespace = ] { ‘DATABASE’ | ‘SERVER’ | NULL } ]
Frosty
+4  A: 

If your at the point of worrying about trigger orders then you really should take a step backwards and consider what you are trying to do and is there a better way of doing it. The fact that this isn't an easy thing to change should be telling you something.

Triggers always look like a real neat solution, and in the right place they are highly valuable, but the price is it's really easy to create debugging nightmares with them. I've lost many hours in the past trying to debug some obscure database behavior only to find the cause is burrowed away in an overlooked trigger.

Cruachan
+5  A: 

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.

JosephStyons
+3  A: 

You can use sp_settriggerorder to define the order of each trigger on a table.

However, I would argue that you'd be much better off having a single trigger that does multiple things. This is particularly so if the order is important, since that importance will not be very obvious if you have multiple triggers. Imagine someone trying to support the database months/years down the track. Of course there are likely to be cases where you need to have multiple triggers or it really is better design, but I'd start assuming you should have one and work from there.

Rory