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.

happen the second time around. Recursion, unlike nesting, is a database-level option and can be set<br />

using the sp_dboption system sproc.<br />

The danger in recursive triggers is that you’ll get into some form of unintended loop. As such, you’ll<br />

need to make sure that you get some form of recursion check in place to stop the process if necessary.<br />

Triggers Don’t Prevent Architectural Changes<br />

This is a classic good news/bad news story.<br />

Using triggers is positively great in terms of making it easy to make architecture changes. Indeed, I often<br />

use triggers for referential integrity early in the development cycle (when I’m more likely to be making<br />

lots of changes to the design of the database), and then change to DRI late in the cycle when I’m close to<br />

production.<br />

When you want to drop a table and re-create it using DRI, you must first drop all the constraints before<br />

dropping the table. This can create quite a maze in terms of dropping multiple constraints, making your<br />

changes, and then adding the constraints again. It can be quite a wild ride trying to make sure that<br />

everything drops that is supposed to so that your changed scripts will run. Then it’s just as wild a ride to<br />

make sure that you’ve got everything back on that needs to be. Triggers take care of all this because they<br />

don’t care that anything has changed until they actually run. There’s the rub, though — when they run.<br />

You see, it means that you may change architecture and break several triggers without even realizing that<br />

you’ve done it. It won’t be until the first time that those triggers try to address the object(s) in question<br />

that you find the error of your ways. By that time, you may find together exactly what you did and why.<br />

Both sides have their hassles — just keep the hassles in mind no matter which method you’re employing.<br />

Triggers Can Be Turned Off Without Being Removed<br />

Sometimes, just like with CHECK constraints, you want to turn off the integrity feature so you can perform<br />

some valid action that will violate the constraint. (Importation of data is probably the most common of<br />

these.)<br />

Another common reason for doing this is when you are performing some sort of bulk insert (importation<br />

again), but you are already 100 percent certain the data is valid. In this case, you may want to turn off<br />

the triggers to eliminate their overhead and speed up the insert process.<br />

You can turn a trigger off and on by using an ALTER TABLE statement. The syntax looks like this:<br />

ALTER TABLE <br />

TRIGGER <br />

Chapter 15: Triggers<br />

As you might expect, my biggest words of caution in this area are, don’t forget to re-enable your triggers!<br />

One last thing. If you’re turning them off to do some form of mass importation of data, I highly recommend<br />

that you kick out all your users and go either to single-user mode, dbo-only mode, or both. This<br />

will ensure that no one sneaks in behind you while you have the triggers turned off.<br />

463

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

Saved successfully!

Ooh no, something went wrong!