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.

<strong>SQL</strong> <strong>Server</strong> 6.5 and prior did not allow multiple triggers of the same type on any single table. For example,<br />

if you had already declared a trigger called trgCheck to enforce data integrity on updates and inserts,<br />

then you couldn’t create a separate trigger for cascading updates. Once one update (or insert, or delete)<br />

trigger was created, that was it — you couldn’t create another trigger for the same type of action. WITH<br />

APPEND gets around this problem by explicitly telling <strong>SQL</strong> <strong>Server</strong> that we want to add this new trigger<br />

even though we already have a trigger of that type on the table — both will be fired when the appropriate<br />

trigger action (INSERT, UPDATE, DELETE) occurs. It’s a way of having a bit of both worlds.<br />

NOT FOR REPLICATION<br />

AS<br />

Adding this option slightly alters the rules for when the trigger is fired. With this option in place, the trigger<br />

will not be fired whenever a replication-related task modifies your table. Usually a trigger is fired (to do<br />

the housekeeping, cascading, and so on) when the original table is modified and there is often no point<br />

in doing it again.<br />

Exactly as it was with sprocs, this is the meat of the matter. The AS keyword tells <strong>SQL</strong> <strong>Server</strong> that your<br />

code is about to start. From this point forward, we’re into the scripted portion of your trigger.<br />

Using Triggers for Data Integrity Rules<br />

Although they shouldn’t be your first option, triggers can also perform the same functionality as a CHECK<br />

constraint or even a DEFAULT. The answer to the question “Should I use triggers vs. CHECK constraints?”<br />

is the rather definitive: “It depends.” If a CHECK can do the job, then it’s probably the preferable choice.<br />

There are times, however, when a CHECK constraint just won’t do the job, or when something inherent in<br />

the CHECK process makes it less desirable than a trigger. Examples of where you would want to use a<br />

trigger over a CHECK include:<br />

❑ Your business rule needs to reference data in a separate table.<br />

❑ Your business rule needs to check the delta (difference between before and after) of an UPDATE.<br />

❑ You require a customized error message.<br />

Chapter 15: Triggers<br />

A summary table of when to use what type of data integrity mechanism is provided at the end of<br />

Chapter 6.<br />

This really just scratches the surface of things. Since triggers are highly flexible, deciding when to use<br />

them really just comes down to balancing your need to get something special done with the overhead<br />

triggers incur and other approaches you might take to address your need.<br />

Dealing with Requirements Sourced from Other Tables<br />

CHECK constraints are great — fast and efficient — but they don’t do everything you’d like them to. Perhaps<br />

the biggest shortcoming shows up when you need to verify data across tables.<br />

457

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

Saved successfully!

Ooh no, something went wrong!