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

the world’s problems. The right answer in this respect is that triggers can do a lot for you, but they can<br />

also cause a lot of problems. The trick is to use them when they are the right things to use, and not to use<br />

them when they aren’t.<br />

Some common uses of triggers include:<br />

❑ Enforcing referential integrity: Although I recommend using Declarative Referential Integrity<br />

(DRI) whenever possible, there are many things that DRI won’t do (for example, referential<br />

integrity across databases or even servers, many complex types of relationships, and so on).<br />

❑ Creating audit trails, which means writing out records that keep track of not just the most current<br />

data, but also the actual change history for each record. This may become less popular with<br />

the new change-data tracking that <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong> adds, but triggers are the way it has been done<br />

historically.<br />

❑ Cresting functionality similar to a CHECK constraint, but which works across tables, databases,<br />

or even servers.<br />

❑ Substituting your own statements in the place of a user’s action statement (usually used to<br />

enable inserts in complex views).<br />

In addition, you have the new, but likely much more rare case (like I said, they are new, so only time will<br />

tell for sure), Data Definition Language (DDL) trigger, which is about monitoring changes in the structure<br />

of your table.<br />

And these are just a few. So, with no further ado, let’s look at exactly what a trigger is.<br />

What Is a T rigger?<br />

452<br />

A trigger is a special kind of stored procedure that responds to specific events. There are two kinds of<br />

triggers: Data Definition Language (DDL) triggers and Data Manipulation Language (DML) triggers.<br />

DDL triggers fire in response to someone changing the structure of your database in some way (CREATE,<br />

ALTER, DROP, and similar statements). These are critical to some installations (particularly high-security<br />

installations), but are pretty narrow in use. In general, you will only need to look into using these where<br />

you need extreme auditing of changes/history of your database structure. Their use is a fairly advanced<br />

concept and, as such, I’m covering them here as mostly a “be aware these exist” thing, and we’ll move<br />

on to the meatier version of triggers.<br />

DML triggers are pieces of code that you attach to a particular table or view. Unlike sprocs, where<br />

you need to explicitly invoke the code, the code in triggers is automatically run whenever the event(s) you<br />

attached the trigger to occur in the table. Indeed, you can’t explicitly invoke triggers — the only way to<br />

do this is by performing the required action in the table that they are assigned to.<br />

Beyond not being able to explicitly invoke a trigger, you’ll find two other things that exist for sprocs but<br />

are missing from triggers: parameters and return codes.

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

Saved successfully!

Ooh no, something went wrong!