Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 10: Views 306 soh.SalesOrderID, soh.OrderDate, sod.ProductID, pp.Name, sod.OrderQty, sod.UnitPrice, sod.UnitPriceDiscount * sod.UnitPrice * sod.OrderQty AS TotalDiscount, sod.LineTotal FROM Sales.Customer AS sc INNER JOIN Sales.SalesOrderHeader AS soh ON sc.CustomerID = soh.CustomerID INNER JOIN Sales.SalesOrderDetail AS sod ON soh.SalesOrderID = sod.SalesOrderID INNER JOIN Production.Product AS pp ON sod.ProductID = pp.ProductID; Now do a SELECT: SELECT * FROM CustomerOrders_vw; You wind up with a bunch of rows — over 121,000 — but you also wind up with information that is far simpler for the average manager to comprehend and sort out. What’s more, with not that much training, the manager (or whoever the user might be) can get right to the heart of what they are looking for: SELECT AccountNumber, LineTotal FROM CustomerOrders_vw WHERE OrderDate = ‘01/07/2002’; The user didn’t need to know how to do a four-table join — that was hidden in the view. Instead, they only need limited skill (and limited imagination for that matter) in order to get the job done. AccountNumber LineTotal ------------- --------------------------------------- AW00014937 3578.270000 AW00018710 3578.270000 AW00025713 699.098200 AW00020558 699.098200 (4 row(s) affected) However, we could make our query even more targeted. Let’s say that we only want our view to return yesterday’s sales. We’ll make only slight changes to our query: USE AdventureWorks2008 GO CREATE VIEW YesterdaysOrders_vw AS SELECT sc.AccountNumber, soh.SalesOrderID,

soh.OrderDate, sod.ProductID, pp.Name, sod.OrderQty, sod.UnitPrice, sod.UnitPriceDiscount * sod.UnitPrice * sod.OrderQty AS TotalDiscount, sod.LineTotal FROM Sales.Customer AS sc INNER JOIN Sales.SalesOrderHeader AS soh ON sc.CustomerID = soh.CustomerID INNER JOIN Sales.SalesOrderDetail AS sod ON soh.SalesOrderID = sod.SalesOrderID INNER JOIN Production.Product AS pp ON sod.ProductID = pp.ProductID WHERE CAST(soh.OrderDate AS Date) = CAST(DATEADD(day,-1,GETDATE()) AS Date); All the dates in the AdventureWorks2008 database are old enough that this view wouldn’t return any data, so let’s modify a few existing rows to test it: USE AdventureWorks2008 UPDATE Sales.SalesOrderHeader SET OrderDate = CAST(DATEADD(day,-1,GETDATE()) AS Date), DueDate = CAST(DATEADD(day,11,GETDATE()) AS Date), ShipDate = CAST(DATEADD(day,6,GETDATE()) AS Date) WHERE Sales.SalesOrderID BETWEEN 43659 AND 43662; The core of this is a relatively simple update statement that is resetting the dates on a few orders to be relative to yesterday (the day before whatever day you run that update statement). The GETDATE() function is, just as you would expect, getting your current date. We’ll discuss some of the other pieces in a bit. For now, I’ll ask that you just take this one largely on faith, and trust that you’ll need to run this to have a value in AdventureWorks2008 that will come up for our view. You should see a result from the Management Studio that looks something like this: (31,465 row(s) affected) Be aware that the message will appear on the Messages tab only if you are using the Management Studio’s Results In Grid mode. It should show up in your Results table if you’re in text mode. The OrderID might vary, but the rest should hold pretty true. Now let’s run a query against your view and see what we get: SELECT AccountNumber, SalesOrderID, OrderDate FROM YesterdaysOrders_vw; You can see that our four orders do show up. Indeed, since each has several line items, you should wind up with a total of 121,317 rows: AccountNumber SalesOrderID OrderDate ------------- ------------ ----------------------- AW00000676 43659 2007-09-30 00:00:00.000 Chapter 10: Views 307

Chapter 10: Views<br />

306<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<br />

FROM Sales.Customer AS sc<br />

INNER JOIN Sales.SalesOrderHeader AS soh<br />

ON sc.CustomerID = soh.CustomerID<br />

INNER JOIN Sales.SalesOrderDetail AS sod<br />

ON soh.SalesOrderID = sod.SalesOrderID<br />

INNER JOIN Production.Product AS pp<br />

ON sod.ProductID = pp.ProductID;<br />

Now do a SELECT:<br />

SELECT *<br />

FROM CustomerOrders_vw;<br />

You wind up with a bunch of rows — over 121,000 — but you also wind up with information that is far<br />

simpler for the average manager to comprehend and sort out. What’s more, with not that much training,<br />

the manager (or whoever the user might be) can get right to the heart of what they are looking for:<br />

SELECT AccountNumber, LineTotal<br />

FROM CustomerOrders_vw<br />

WHERE OrderDate = ‘01/07/2002’;<br />

The user didn’t need to know how to do a four-table join — that was hidden in the view. Instead, they<br />

only need limited skill (and limited imagination for that matter) in order to get the job done.<br />

AccountNumber LineTotal<br />

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

AW00014937 3578.270000<br />

AW00018710 3578.270000<br />

AW00025713 699.098200<br />

AW00020558 699.098200<br />

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

However, we could make our query even more targeted. Let’s say that we only want our view to return<br />

yesterday’s sales. We’ll make only slight changes to our query:<br />

USE AdventureWorks<strong>2008</strong><br />

GO<br />

CREATE VIEW YesterdaysOrders_vw<br />

AS<br />

SELECT sc.AccountNumber,<br />

soh.SalesOrderID,

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

Saved successfully!

Ooh no, something went wrong!