17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The syntax of sp_settriggerorder looks like this:<br />

sp_settriggerorder[@triggername =] ‘’,<br />

[@order =] ‘{FIRST|LAST|NONE}’,<br />

[@stmttype =] ‘{INSERT|UPDATE|DELETE}’<br />

[, [@namespace =] {‘DATABASE’ | ‘SERVER’ | NULL} ]<br />

There can be only one trigger that is considered to be FIRST for any particular action (INSERT, UPDATE,<br />

or DELETE). Likewise, there can be only one LAST trigger for any particular action. Any number of triggers<br />

can be considered to be NONE — that is, the number of triggers that don’t have a particular firing<br />

order is unlimited.<br />

So, the question should be, why do I care what order they fire in? Well, often you won’t care at all. At<br />

other times, it can be important logic-wise or just a good performance idea. Let’s consider what I mean<br />

in a bit more detail.<br />

Controlling Firing Order for Logic Reasons<br />

Why would you need to have one trigger fire before another? The most common reason would be that<br />

the first trigger lays some sort of foundation for, or otherwise validates, what will come afterwards.<br />

Separate triggers also allow one part of the code to be disabled (remember that NO CHECK thing we did<br />

a few sections ago?) while other parts of the code continue to function. The downside is that, if you go<br />

ahead and separate your code into separate triggers, you lose the logical stepping order that the code<br />

had when it was in one trigger.<br />

By having at least a simple level of control over firing order, we have something of the best of both<br />

worlds — we can logically separate our triggers, but still maintain the necessary order of precedence on<br />

what piece of code runs first or last.<br />

Controlling Firing Order for Performance Reasons<br />

On the performance front, a FIRST trigger is the only one that really has any big thing going for it. If you<br />

have multiple triggers, but only one of them is likely to generate a rollback (for example, it may be enforcing<br />

a complex data integrity rule that a constraint can’t handle), you would want to consider making such a<br />

trigger a FIRST trigger. This ensures that your most likely cause of a rollback is already considered before<br />

you invest any more activity in your transaction. The more you do before the rollback is detected, the<br />

more that will have to be rolled back. Get the highest probability of a rollback happening determined<br />

before performing additional activity.<br />

INSTEAD OF T riggers<br />

Chapter 15: Triggers<br />

INSTEAD OF triggers were added in <strong>SQL</strong> <strong>Server</strong> 2000 and remain one of the more complex features of<br />

<strong>SQL</strong> <strong>Server</strong>. While it is well outside the scope of a “beginning” concept, I’m still a big believer in even<br />

the beginner learning about what things are available, and so we’ll touch on what these are about here.<br />

Essentially, an INSTEAD OF trigger is a block of code we can use as an interceptor for anything that anyone<br />

tries to do to our table or view. We can either go ahead and do whatever the user requests or, if we<br />

choose, we can go so far as doing something that is entirely different.<br />

465

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!