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.

Chapter 15: Triggers<br />

Like regular triggers, INSTEAD OF triggers come in three different flavors: INSERT, UPDATE, and<br />

DELETE. In each case, the most common use is the same — resolving ambiguity about which table(s) are<br />

to receive the actual changes when you’re dealing with a view based on multiple tables.<br />

Performance Considerations<br />

I’ve seen what appear almost like holy wars happen over the pros and cons, evil and good, and light and<br />

dark of triggers. The worst of it tends to come from purists — people who love the theory, and that’s all<br />

they want to deal with, or people that have figured out how flexible triggers are and want to use them<br />

for seemingly everything.<br />

My two-bits worth on this is, as I stated early in the chapter, use them when they are the right things to<br />

use. If that sounds sort of noncommittal and ambiguous — good! Programming is rarely black and white,<br />

and databases are almost never that way. I will, however, point out some facts for you to think about.<br />

Triggers Are Reactive Rather Than Proactive<br />

What I mean here is that triggers happen after the fact. By the time that your trigger fires, the entire query<br />

has run and your transaction has been logged (but not committed and only to the point of the statement<br />

that fired your trigger). This means that if the trigger needs to roll things back, it has to undo what is<br />

potentially a ton of work that’s already been done. Slow! Keep this knowledge in balance though. How<br />

big an impact this adds up to depends strongly on how big your query is.<br />

So what, you say. Well, compare this to the notion of constraints, which are proactive — that is, they happen<br />

before your statement is really executed. That means they detect things that will fail and prevent<br />

them from happening earlier in the process. This will usually mean that they will run at least slightly<br />

faster — much faster on more complex queries. Note that this extra speed really shows itself to any significant<br />

extent only when a rollback occurs.<br />

What’s the end analysis here? Well, if you’re dealing with very few rollbacks, and/or the complexity and<br />

execution time of the statements affected are low, then there probably isn’t much of a difference between<br />

triggers and constraints. There’s some, but probably not much. If, however, the number of rollbacks is<br />

unpredictable or you know it’s going to be high, you’ll want to stick with constraints if you can (and<br />

frankly, I suggest sticking with constraints unless you have a very specific reason not to).<br />

Triggers Don’t Have Concurrency Issues<br />

with the Process That Fires Them<br />

466<br />

You may have noticed throughout this chapter that we often make use of the ROLLBACK statement, even<br />

though we don’t issue a BEGIN TRAN. That’s because a trigger is always implicitly part of the same transaction<br />

as the statement that caused the trigger to fire.<br />

If the firing statement was not part of an explicit transaction (one where there was a BEGIN TRAN), then<br />

it would still be part of its own one-statement transaction. In either case, a ROLLBACK TRAN issued inside<br />

the trigger will still roll back the entire transaction.

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

Saved successfully!

Ooh no, something went wrong!