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 WITH ENCRYPTION This works just as it does for views and sprocs. If you add this option, you can be certain that no one will be able to view your code (not even you!). This is particularly useful if you are going to be building software for commercial distribution, or if you are concerned about security and don’t want your users to be able to see what data you’re modifying or accessing. Obviously, you should keep a copy of the code required to create the trigger somewhere else, in case you want to re-create it sometime later. As with views and sprocs, the thing to remember when using the WITH ENCRYPTION option is that you must reapply it every time you use ALTER on your trigger. If you make use of an ALTER TRIGGER statement and do not include the WITH ENCRYPTION option, then the trigger will no longer be encrypted. The FOR|AFTER vs. the INSTEAD OF Clause 454 In addition to deciding what kind of queries will fire your trigger (INSERT, UPDATE, and/or DELETE), you also have some choice as to when the trigger fires. While the FOR (alternatively, you can use the keyword AFTER if you choose) trigger is the one that has been around a long time and that people generally think of, you also have the ability to run what is called an INSTEAD OF trigger. Which of these two you use will affect whether you enter your trigger before the data has been modified or after. In either case, you will be in your trigger before any changes are truly committed to the database. Confusing? Probably. Let’s try it a different way with a diagram that shows where each choice fires (see Figure 15-1). The thing to note here is that, regardless of which choice you make, SQL Server will put together two working tables — one holding a copy of the records that were inserted (and, incidentally, called Inserted) and one holding a copy of any records that were deleted (called Deleted). We’ll look into the uses of these working tables a little later. For now realize that, with INSTEAD OF triggers, the creation of these working tables will happen before any constraints are checked, with FOR triggers, these tables will be created after constraints are checked. The key to INSTEAD OF triggers is that you can actually run your own code in the place of whatever the user requested. This means we can clean up ambiguous INSERT problems in views. It also means that we can take action to clean up constraint violations before the constraint is even checked. As positively glorious as this sounds, this is actually pretty complex stuff. It means that you need to anticipate every possibility. In addition, it means that you are effectively adding a preprocess (a process that runs before the main code) to every query that changes data in any way for this table (this is not a good thing performance wise). Cool as they sound, INSTEAD OF triggers fall in the category of fairly advanced stuff and are well outside the scope of this book. Triggers using the FOR and AFTER declaration behave identically to each other. The big difference between them and INSTEAD OF triggers is that they build their working tables after any constraints have been checked.
Statement issued: “INSERT INTO...” Begin transaction (if one hasn’t been explicitly defined) Does an INSTEAD OF trigger exist? Figure 15-1 No Check contraints Log statement Populate inserted and deleted tables Fire FOR/AFTER triggers Commit transaction (if one hasn’t been explicitly defined) All done! Yes Populate inserted and deleted tables Yes INSTEAD OF trigger fired. *IF EXISTS... SELECT... INSERT... ... ... Did INSTEAD OF trigger perform a similar action on the table? No Chapter 15: Triggers Actions on other objects 455
- Page 442 and 443: Chapter 12: Stored Procedures match
- Page 444 and 445: Chapter 12: Stored Procedures There
- Page 446 and 447: Chapter 12: Stored Procedures 408 f
- Page 449 and 450: 13 User-Defined Functions Well, her
- Page 451 and 452: types!), except for BLOBs, cursors,
- Page 453 and 454: We get back the same set as with th
- Page 455 and 456: AS RETURN (SELECT BusinessEntityID,
- 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 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 508 and 509: Chapter 15: Triggers This is the sa
- 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
Statement issued:<br />
“INSERT INTO...”<br />
Begin transaction<br />
(if one hasn’t<br />
been explicitly<br />
defined)<br />
Does an INSTEAD<br />
OF trigger exist?<br />
Figure 15-1<br />
No<br />
Check contraints<br />
Log statement<br />
Populate inserted<br />
and deleted tables<br />
Fire FOR/AFTER<br />
triggers<br />
Commit transaction<br />
(if one hasn’t been<br />
explicitly defined)<br />
All done!<br />
Yes<br />
Populate inserted<br />
and deleted tables<br />
Yes<br />
INSTEAD OF<br />
trigger fired.<br />
*IF EXISTS...<br />
SELECT...<br />
INSERT...<br />
...<br />
...<br />
Did<br />
INSTEAD OF<br />
trigger<br />
perform a similar<br />
action on the<br />
table?<br />
No<br />
Chapter 15: Triggers<br />
Actions on<br />
other objects<br />
455