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.

Now try to update one of the rows using the view — set the PostalCode to anything other than a value<br />

starting with 97 or 98:<br />

UPDATE PortlandAreaAddresses_vw<br />

SET PostalCode = ‘33333’ -- it was 97205<br />

WHERE AddressID = 22;<br />

<strong>SQL</strong> <strong>Server</strong> promptly tells you that you are a scoundrel and that you should be burned at the stake for<br />

your actions — well, not really, but it does make its point:<br />

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

The attempted insert or update failed because the target view either specifies WITH<br />

CHECK OPTION or spans a view that specifies WITH CHECK OPTION and one or more rows<br />

resulting from the operation did not qualify under the CHECK OPTION constraint.<br />

The statement has been terminated.<br />

How It Works<br />

Our WHERE clause filters things in the view to show only postal codes that start with 970, 971, 972, or<br />

9866-9869, and the WITH CHECK OPTION says any INSERT or UPDATE statements must meet that WHERE<br />

clause criteria (which a (33333) postal code doesn’t).<br />

Since our update wouldn’t meet the WHERE clause criteria, it is thrown out; however, if we were to<br />

update the row right in the base table:<br />

UPDATE Person.Address<br />

SET PostalCode = ‘33333’ -- it was 97205<br />

WHERE AddressID = 22;<br />

<strong>SQL</strong> <strong>Server</strong> is a lot friendlier:<br />

(1 row(s) affected)<br />

Chapter 10: Views<br />

The restriction applies only to the view — not to the underlying table. This can actually be quite handy<br />

in a rare circumstance or two. Imagine a situation where you want to allow some users to insert or update<br />

data in a table, but only when the updated or inserted data meets certain criteria. We could easily deal<br />

with this restriction by adding a CHECK constraint to our underlying table — but this might not always<br />

be an ideal solution.<br />

Imagine now that we’ve added a second requirement — we still want other users to be able to INSERT<br />

data into the table without meeting these criteria. Uh oh, the CHECK constraint will not discriminate<br />

between users. By using a view together with a WITH CHECK OPTION, we can point the restricted users<br />

to the view, and let the unrestricted users make use of the base table or a view that has no such restriction.<br />

Note that this works on an INSERT, too. Run an INSERT that violates the WHERE clause and you’ll see<br />

your old friend, the “terminator” error, exactly as we did with the UPDATE.<br />

311

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

Saved successfully!

Ooh no, something went wrong!