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 />

This is the same notion as the “Short and Sweet” section — long-running queries make for long-running<br />

statements which, in turn, lead to long-running everything. Don’t forget your triggers when you optimize!<br />

Try Not to Roll Back Within Triggers<br />

This one’s hard since rollbacks are so often a major part of what you want to accomplish with your<br />

triggers.<br />

Just remember that AFTER triggers (which are far and away the most common type of trigger) happen<br />

after most of the work is already done — that means a rollback is expensive. This is where DRI picks up<br />

almost all of its performance advantage. If you are using many ROLLBACK TRAN statements in your triggers,<br />

then make sure that you preprocess looking for errors before you execute the statement that fires<br />

the trigger. Because <strong>SQL</strong> <strong>Server</strong> can’t be proactive in this situation, you need to be proactive for it. Test<br />

for errors beforehand rather than waiting for the rollback.<br />

Dropping Triggers<br />

Dropping triggers works only slightly differently than it has worked for almost everything else thus far.<br />

The only real trick is that, like tables, trigger names are scoped at the schema level. This means that you<br />

can have two objects with a trigger of the same name, as long as the object the trigger is placed against it<br />

in a different schema than the other trigger of the same name. Restated, the trigger is named in terms of<br />

the schema it’s in, rather than the object it is associated with — odd when you realize it is subsidiary to<br />

the table or view it is attached to. The syntax looks like:<br />

DROP TRIGGER [.]<br />

Other than the schema issue, dropping triggers is pretty much the same as any other drop.<br />

Debugging Triggers<br />

470<br />

Most everything to do with the debugger works the same for triggers as it does for anything else. The<br />

only real trick is a result of the fact that you can’t directly call triggers in the way you can scripts or<br />

stored procedures.<br />

Fortunately, most developer types are reasonably intelligent people, so, if you think about it, you probably<br />

can anticipate how we can get around starting the debugger for triggers — we just need to debug a<br />

statement that causes a trigger to fire.<br />

In releases prior to <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong>, direct debugging of scripts wasn’t available in 2005 and earlier.<br />

This meant you needed to create a stored procedure that would cause a trigger to be debugged if you<br />

wanted to debug that trigger — happily, this is no longer the case.<br />

Since we reviewed the basics of debugging fairly thoroughly in Chapter 12, let’s cut right to the proverbial<br />

chase and look at debugging triggers in action. All we need to do to show this off is run a bit of code

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

Saved successfully!

Ooh no, something went wrong!