Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 457 and 458: in your relational database. These
- Page 459 and 460: AS BEGIN ( EmployeeID int NOT NULL,
- Page 461 and 462: So, as you can see, we can actually
- Page 463 and 464: Despite being schema-bound, this on
- Page 465 and 466: 14 Transactions and Locks This is o
- Page 467 and 468: we are unable or do not want to com
- Page 469 and 470: Figure 14-1 Data needed Data in cac
- Page 471 and 472: Transaction 4 This transaction wasn
- Page 473 and 474: Oops — problem!!! Transaction 2 h
- Page 475 and 476: The only cure for this is setting y
- Page 477 and 478: Exclusive Locks Exclusive locks are
- Page 479 and 480: Also: ❑ The Sch-S is compatible w
- Page 481 and 482: The syntax for switching between th
- Page 483 and 484: As with most things in life, howeve
- Page 485 and 486: purchased. Process 2 records sales;
- Page 487: Chapter 14: Transactions and Locks
- Page 490 and 491: Chapter 15: Triggers the world’s
- Page 492 and 493: Chapter 15: Triggers WITH ENCRYPTIO
- Page 494 and 495: Chapter 15: Triggers FOR|AFTER The
- Page 496 and 497: Chapter 15: Triggers 458 To illustr
- Page 498 and 499: Chapter 15: Triggers 460 IF EXISTS
- Page 500 and 501: Chapter 15: Triggers ❑ Feeding de
- Page 502 and 503: Chapter 15: Triggers Trigger Firing
- Page 504 and 505: Chapter 15: Triggers Like regular t
- Page 506 and 507: Chapter 15: Triggers The COLUMNS_UP
- Page 510 and 511: Chapter 15: Triggers 472 we have th
- Page 512 and 513: Chapter 16: A Brief XML Primer So,
- Page 514 and 515: Chapter 16: A Brief XML Primer Figu
- Page 516 and 517: Chapter 16: A Brief XML Primer Elem
- Page 518 and 519: Chapter 16: A Brief XML Primer ❑
- Page 520 and 521: Chapter 16: A Brief XML Primer 482
- Page 522 and 523: Chapter 16: A Brief XML Primer Name
- Page 524 and 525: Chapter 16: A Brief XML Primer The
- Page 526 and 527: Chapter 16: A Brief XML Primer 2. W
- Page 528 and 529: Chapter 16: A Brief XML Primer SQL
- Page 530 and 531: Chapter 16: A Brief XML Primer So,
- Page 532 and 533: Chapter 16: A Brief XML Primer If,
- Page 534 and 535: Chapter 16: A Brief XML Primer 496
- Page 536 and 537: Chapter 16: A Brief XML Primer Note
- Page 538 and 539: Chapter 16: A Brief XML Primer RAW
- Page 540 and 541: Chapter 16: A Brief XML Primer AUTO
- Page 542 and 543: Chapter 16: A Brief XML Primer EXPL
- Page 544 and 545: Chapter 16: A Brief XML Primer Chec
- Page 546 and 547: Chapter 16: A Brief XML Primer 508
- Page 548 and 549: Chapter 16: A Brief XML Primer 510
- Page 550 and 551: Chapter 16: A Brief XML Primer 512
- Page 552 and 553: Chapter 16: A Brief XML Primer The
- Page 554 and 555: Chapter 16: A Brief XML Primer 516
- Page 556 and 557: Chapter 17: Reporting for Duty, Sir
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