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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

I just picked out “Adjustable Race” as our victim, but you could have chosen any ProductID, as long as<br />

you set the value to less than 50 percent of its previous value. If you do, you’ll get the expected error:<br />

Msg 50000, Level 16, State 1, Procedure ProductIsRationed, Line 16<br />

Cannot reduce stock by more than 50% at once.<br />

Msg 3609, Level 16, State 1, Line 1<br />

The transaction ended in the trigger. The batch has been aborted.<br />

Note that we could have also implemented this in the SalesOrderDetail table by referencing the actual<br />

order quantity against the current Quantity amount, but we would have run into several problems:<br />

❑ Updates that change: Is the process that’s creating the SalesOrderDetail record updating<br />

ProductInventory before or after the SalesOrderDetail record? That makes a difference in<br />

how we make use of the Quantity value in the ProductInventory table to calculate the effect<br />

of the transaction.<br />

❑ The inventory external to the SalesOrderDetails table updates would not be affected: They<br />

could still reduce the inventory by more than half (this may actually be a good thing in many<br />

circumstances, but it’s something that has to be thought about).<br />

Using Triggers for Custom Error Messages<br />

We’ve already touched on this in some of our other examples, but remember that triggers can be handy<br />

for when you want control over the error message or number that gets passed out to your user or client<br />

application.<br />

With a CHECK constraint for example, you’re just going to get the standard 547 error along with its rather<br />

nondescript explanation. As often as not, this is less than helpful in terms of the user really figuring out<br />

what went wrong — indeed, your client application often doesn’t have enough information to make an<br />

intelligent and helpful response on behalf of the user.<br />

In short, sometimes you create triggers when there is already something that would give you the data<br />

integrity that you want, but won’t give you enough information to handle it.<br />

It’s worth noting that the need for custom error messages in <strong>SQL</strong> <strong>Server</strong> should be relatively rare, although<br />

passing custom error codes is often useful. Why not pass the custom error message? Well, one would<br />

think that you probably have an application layer on top of it, and it is likely going to want to put more<br />

context on the error anyway, so the <strong>SQL</strong>-<strong>Server</strong>-specific text may not be all that useful. Using a special<br />

error code may, however, be very useful to your application in terms of determining what exactly happened<br />

and applying the correct client-side error handling code.<br />

Other Common Uses for T riggers<br />

In addition to the straight data integrity uses, triggers have a number of other uses. Indeed, the possibilities<br />

are fairly limitless, but here are a few common examples:<br />

❑ Updating summary information<br />

Chapter 15: Triggers<br />

461

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

Saved successfully!

Ooh no, something went wrong!