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

❑ Feeding de-normalized tables for reporting<br />

❑ Setting condition flags<br />

As you can see, the possibilities are pretty far reaching — it’s really all about your particular situation<br />

and the needs of your particular system.<br />

Other Trigger Issues<br />

You have most of it now but if you’re thinking you are finished with triggers, then think again. As I indicated<br />

early in the chapter, triggers create an awful lot to think about. The sections that follow attempt to<br />

point out some of the biggest issues you need to consider, plus provide some information on additional<br />

trigger features and possibilities.<br />

Triggers Can Be Nested<br />

A nested trigger is one that did not fire directly as a result of a statement that you issued, but rather because<br />

of a statement that was issued by another trigger.<br />

This can actually set off quite a chain of events, with one trigger causing another trigger to fire which, in<br />

turn, causes yet another trigger to fire, and so on. Just how deep the triggers can fire depends on:<br />

❑ Whether nested triggers are turned on for your system (This is a system-wide, not databaselevel<br />

option; it is set using the Management Studio or sp_configure, and defaults to on.)<br />

❑ Whether there is a limit of nesting to 32 levels deep.<br />

❑ Whether a trigger has already been fired. A trigger can, by default, be fired only once per trigger<br />

transaction. Once fired, it will ignore any other calls as a result of activity that is part of the same<br />

trigger action. Once you move on to an entirely new statement (even within the same overall<br />

transaction), the process can start all over again.<br />

In most circumstances, you actually want your triggers to nest (thus the default), but you need to think<br />

about what’s going to happen if you get into a circle of triggers firing triggers. If it comes back around to<br />

the same table twice, then the trigger will not fire the second time, and something you think is important<br />

may not happen; for example, a data integrity violation may get through. It’s also worth noting that, if<br />

you do a ROLLBACK anywhere in the nesting chain, the entire chain is rolled back. In other words, the<br />

entire nested trigger chain behaves as a transaction.<br />

Triggers Can Be Recursive<br />

462<br />

What is a recursive trigger? A trigger is said to be recursive when something the trigger does eventually<br />

causes that same trigger to be fired. It may be directly (by an action query done to the table on which the<br />

trigger is set) or indirectly (through the nesting process).<br />

Recursive triggers are rare. Indeed, by default, recursive triggers are turned off. This is, however, a way<br />

of dealing with the situation just described where you are nesting triggers and you want the update to

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

Saved successfully!

Ooh no, something went wrong!