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.

Let’s go ahead and test out our handiwork. First, we need a record or two that will fail when it hits our<br />

trigger. Unfortunately, AdventureWorks<strong>2008</strong> has apparently never discontinued a product before — we’ll<br />

just have to change that…<br />

UPDATE Production.Product<br />

SET DiscontinuedDate = ‘01-01-<strong>2008</strong>’<br />

WHERE ProductID = 680<br />

Now, knowing that we have a discontinued product, let’s go ahead and add an order detail item that<br />

violates the constraint. Note that I’ve chosen an existing order (43659) to tack this item onto:<br />

INSERT INTO Sales.SalesOrderDetail<br />

VALUES<br />

(43659, ‘4911-403C-98’, 1, 680, 1, 1431.50,0.00, NEWID(), GETDATE())<br />

This gets the rejection that we expect:<br />

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

Order Item is discontinued. Transaction Failed.<br />

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

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

Remember that we could, if desired, also create a custom error message to raise, instead of the ad hoc<br />

message that we used with the RAISERROR command.<br />

If you’re working along with these, but trying to keep your AdventureWorks<strong>2008</strong> database close to its<br />

original state, don’t forget to return ProductID 680 back to its null discontinued date.<br />

Using Triggers to Check the Delta of an Update<br />

Sometimes, you’re not interested as much in what the value was or is, as you are in how much it changed.<br />

While there isn’t any one column or table that gives you that information, you can calculate it by making<br />

use of the Inserted and Deleted tables in your trigger.<br />

To check this out, let’s take a look at the Production.ProductInventory table. ProductInventory<br />

has a column called Quantity. Recently, there has been a rush on several products, and Adventure-<br />

Works<strong>2008</strong> has been selling out of several things. Since AdventureWorks<strong>2008</strong> needs more than just a few<br />

customers to stay in business in the long run, it has decided to institute a rationing system on their products.<br />

The Inventory department has requested that we prevent orders from being placed that try to sell<br />

more than half of the units in stock for any particular product.<br />

To implement this, we make use of both the Inserted and Deleted tables:<br />

CREATE TRIGGER Production.ProductIsRationed<br />

ON Production.ProductInventory<br />

FOR UPDATE<br />

AS<br />

Chapter 15: Triggers<br />

459

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

Saved successfully!

Ooh no, something went wrong!