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

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

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 15: Triggers This is the same notion as the “Short and Sweet” section — long-running queries make for long-running statements which, in turn, lead to long-running everything. Don’t forget your triggers when you optimize! Try Not to Roll Back Within Triggers This one’s hard since rollbacks are so often a major part of what you want to accomplish with your triggers. Just remember that AFTER triggers (which are far and away the most common type of trigger) happen after most of the work is already done — that means a rollback is expensive. This is where DRI picks up almost all of its performance advantage. If you are using many ROLLBACK TRAN statements in your triggers, then make sure that you preprocess looking for errors before you execute the statement that fires the trigger. Because SQL Server can’t be proactive in this situation, you need to be proactive for it. Test for errors beforehand rather than waiting for the rollback. Dropping Triggers Dropping triggers works only slightly differently than it has worked for almost everything else thus far. The only real trick is that, like tables, trigger names are scoped at the schema level. This means that you can have two objects with a trigger of the same name, as long as the object the trigger is placed against it in a different schema than the other trigger of the same name. Restated, the trigger is named in terms of the schema it’s in, rather than the object it is associated with — odd when you realize it is subsidiary to the table or view it is attached to. The syntax looks like: DROP TRIGGER [.] Other than the schema issue, dropping triggers is pretty much the same as any other drop. Debugging Triggers 470 Most everything to do with the debugger works the same for triggers as it does for anything else. The only real trick is a result of the fact that you can’t directly call triggers in the way you can scripts or stored procedures. Fortunately, most developer types are reasonably intelligent people, so, if you think about it, you probably can anticipate how we can get around starting the debugger for triggers — we just need to debug a statement that causes a trigger to fire. In releases prior to SQL Server 2008, direct debugging of scripts wasn’t available in 2005 and earlier. This meant you needed to create a stored procedure that would cause a trigger to be debugged if you wanted to debug that trigger — happily, this is no longer the case. Since we reviewed the basics of debugging fairly thoroughly in Chapter 12, let’s cut right to the proverbial chase and look at debugging triggers in action. All we need to do to show this off is run a bit of code

that exercises the trigger we most recently worked with in this chapter. We can then step into that script, so we can watch the debugger run through it line by line: BEGIN TRAN -- This one should work UPDATE Production.ProductInventory SET Quantity = 400 -- Was 408 if you want to set it back WHERE ProductID = 1 AND LocationID = 1 -- This one shouldn’t UPDATE Production.ProductInventory SET Quantity = 1 -- Was 408 if you want to set it back WHERE ProductID = 1 AND LocationID = 1 IF @@TRANCOUNT > 0 ROLLBACK TRAN With this script up in the Query Window in Management Studio, we can just press F11 to step into the script (or set a breakpoint and then use any of the several options that start the debugger). We start, of course, at the BEGIN TRAN statement. Continue to step into (F11) until you execute the first UPDATE statement, and note that you wind up in the trigger. Figure 15-6 shows the call stack as you enter the trigger for the first time. Figure 15-6 Now, go ahead and step out of the first UPDATE, and into the second UPDATE. Since the first UPDATE statement didn’t match the violation test in the trigger, you exited almost immediately, but this second time you should see, as you step through, that the code that aborts the transaction fires. In short, we were able to utilize a single script that, in this case, had two statements meant to test the two possible code paths in our trigger. Obviously, you would need more complex scripting to handle the possible code paths for more complex triggers, but, beyond that, the debugging needs are pretty much as they are for testing any other code in T-SQL. Summary Chapter 15: Triggers Triggers are an extremely powerful tool that can add tremendous flexibility to both your data integrity and the overall operation of your system. That being said, they are not something to take lightly. Triggers can greatly enhance the performance of your system if you use them for proper summarization of data, but they can also be the bane of your existence. They can be very difficult to debug (even now that 471

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!