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.

It’s surprising to many, but you can run INSERT, UPDATE, and DELETE statements against a view successfully.<br />

There are several things, however, that you need to keep in mind when changing data through a view:<br />

❑ If the view contains a join, you won’t, in most cases, be able to INSERT or DELETE data unless<br />

you make use of an INSTEAD OF trigger. An UPDATE can, in some cases (as long as you are only<br />

updating columns that are sourced from a single table), work without INSTEAD OF triggers, but<br />

it requires some planning, or you’ll bump into problems very quickly.<br />

❑ If your view references only a single table, then you can INSERT data using a view without the<br />

use of an INSTEAD OF trigger provided all the required fields in the table are exposed in the<br />

view or have defaults. Even for single table views, if there is a column not represented in the<br />

view that does not have a default value, then you must use an INSTEAD OF trigger if you want<br />

to allow an INSERT.<br />

❑ You can, to a limited extent, restrict what is and isn’t inserted or updated in a view.<br />

Now — I’ve already mentioned INSTEAD OF triggers several times. The problem here is the complexity<br />

of INSTEAD OF triggers and that we haven’t discussed triggers to any significant extent yet. As is often<br />

the case in <strong>SQL</strong> <strong>Server</strong> items, we have something of the old chicken vs. egg thing going (“Which came<br />

first?”). I need to discuss INSTEAD OF triggers because of their relevance to views, but we’re also not<br />

ready to talk about INSTEAD OF triggers unless we understand both of the objects (tables and views) that<br />

they can be created against.<br />

The way we are going to handle things for this chapter is to address views the way they used to be —<br />

before there was such a thing as INSTEAD OF triggers. While we won’t deal with the specifics of INSTEAD<br />

OF triggers in this chapter, we’ll make sure we understand when they must be used. We’ll then come<br />

back and address these issues more fully when we look briefly at INSTEAD OF triggers in Chapter 15.<br />

Having said that, I will provide this bit of context — An INSTEAD OF trigger is a special kind of trigger<br />

that essentially runs “instead” of whatever statement caused the trigger to fire. The result is that it<br />

can see what your statement would have done, and then make decisions right in the trigger about how<br />

to resolve any conflicts or other issues that might have come up. It’s very powerful, but also fairly complex<br />

stuff, which is why we defer it for now.<br />

Dealing with Changes in Views with Joined Data<br />

If the view has more than one table, then using a view to modify data is, in many cases, out — sort of<br />

anyway — unless you use an INSTEAD OF trigger (more on this in a moment). Since it creates some<br />

ambiguities in the key arrangements, <strong>Microsoft</strong> locks you out by default when there are multiple tables.<br />

To resolve this, you can use an INSTEAD OF trigger to examine the altered data and explicitly tell <strong>SQL</strong><br />

<strong>Server</strong> what you want to do with it.<br />

Required Fields Must Appear in the View or Have Default Value<br />

Chapter 10: Views<br />

By default, if you are using a view to insert data (there must be a single table SELECT in the underlying<br />

query or at least you must limit the insert to affect just one table and have all required columns represented),<br />

then you must be able to supply some value for all required fields (fields that don’t allow<br />

NULLs). Note that by “supply some value” I don’t mean that it has to be in the SELECT list — a default<br />

covers the bill rather nicely. Just be aware that any columns that do not have defaults and do not accept<br />

309

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

Saved successfully!

Ooh no, something went wrong!