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.

Chapter 10: Views<br />

Editing V iews with T -<strong>SQL</strong><br />

The main thing to remember when you edit views with T-<strong>SQL</strong> is that you are completely replacing the existing<br />

view. The only differences between using the ALTER VIEW statement and the CREATE VIEW statement are:<br />

❑ ALTER VIEW expects to find an existing view, whereas CREATE doesn’t.<br />

❑ ALTER VIEW retains any permissions that have been established for the view.<br />

❑ ALTER VIEW retains any dependency information.<br />

The second of these is the biggie. If you perform a DROP, and then use a CREATE, you have almost the<br />

same effect as using an ALTER VIEW statement. The problem is that you need to entirely re-establish<br />

your permissions on who can and can’t use the view.<br />

Dropping Views<br />

It doesn’t get much easier than this:<br />

DROP VIEW , [,[ ...n]]<br />

And it’s gone.<br />

Creating and Editing V iews in the<br />

Management Studio<br />

312<br />

For people who really don’t know what they are doing, this has to be a rather cool feature in the Management<br />

Studio. Building views is a snap, and you really don’t have to know all that much about queries<br />

in order to get it done.<br />

To take a look at this, fire up the Management Studio, open up the AdventureWorks<strong>2008</strong> database sub-node<br />

of the Databases node and right-click Views. Select New View, and up comes the dialog shown in Figure 10-1.<br />

This dialog makes it easy for us to choose which tables we’re going to be including data from. The<br />

Address table is selected in Figure 10-1, but we’re going to be working with not only a different table —<br />

but four other tables.<br />

This should beg the question: “How do I select more than one table?” Easy — just hold down your Ctrl<br />

key while selecting all the tables you want. For now, start by clicking on the Customer table, and then<br />

press and hold your Ctrl key while you also select the SalesOrderHeader, SalesOrderDetail, and<br />

Product tables. You should wind up with all of them highlighted. Now click Add, and <strong>SQL</strong> <strong>Server</strong> will<br />

add several tables to our view (indeed, on most systems, you should be able to see them being added to<br />

the view editor that we’re about to take a look at).

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

Saved successfully!

Ooh no, something went wrong!