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

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

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

Saved successfully!

Ooh no, something went wrong!