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 Views in the Management Studio<br />

Modifying your view in the Management Studio is as easy as creating it was. The only real difference is<br />

that you need to navigate to your specific view and right-click it — then choose Modify, and you’ll be<br />

greeted with the same friendly query designer that we used with our query when it was created.<br />

Auditing: Displa ying Existing Code<br />

316<br />

What do you do when you have a view, but you’re not sure what it does? The first option should be easy<br />

at this point — just go into the Management Studio like you’re going to edit the view. Go to the Views<br />

sub-node, select the view you want to edit, right-click, and choose Modify View. You’ll see the code<br />

behind the view complete with color-coding.<br />

Unfortunately, we don’t always have the option of having the Management Studio around to hold our<br />

hand through this stuff (we may be using a lighter weight tool of some sort. The bright side is that we<br />

have two reliable ways of getting at the actual view definition:<br />

❑ sp_helptext<br />

❑ The sys.modules metadata function<br />

Using sp_helptext is highly preferable, as when new releases come out, it will automatically be<br />

updated for changes to the system tables.<br />

There is, arguably, a third option: directly accessing the syscomments system table. <strong>Microsoft</strong> has been<br />

warning for a couple of releases now about not using system tables directly. As of this writing, syscomments<br />

is still there, but the results that come out of it when you run a query have some bogus information<br />

in them. They may work fine for you, but, given that <strong>Microsoft</strong> has been recommending against<br />

using syscomments for a while now, it probably makes sense to move on to the more “approved” methods.<br />

Let’s run sp_helptext against one of the views we created in our AdventureWorks<strong>2008</strong> database<br />

earlier in the chapter — YesterdaysOrders_vw:<br />

EXEC sp_helptext YesterdaysOrders_vw;<br />

<strong>SQL</strong> <strong>Server</strong> obliges us with the code for the view:<br />

Text<br />

-----------------------------------------------------------------------<br />

CREATE VIEW YesterdaysOrders_vw<br />

AS<br />

SELECT sc.AccountNumber,<br />

soh.SalesOrderID,<br />

soh.OrderDate,<br />

sod.ProductID,<br />

pp.Name,<br />

sod.OrderQty,<br />

sod.UnitPrice,<br />

sod.UnitPriceDiscount * sod.UnitPrice * sod.OrderQty AS TotalDiscount,<br />

sod.LineTotal

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

Saved successfully!

Ooh no, something went wrong!