Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 293 and 294: Chapter 8: Being Normal: Normalizat
- Page 295 and 296: Chapter 8: Being Normal: Normalizat
- Page 297 and 298: Chapter 8: Being Normal: Normalizat
- Page 299 and 300: 9 SQL Ser ver Storage and Index Str
- Page 301 and 302: Page Splits When a page becomes ful
- Page 303 and 304: The point here is that what happens
- Page 305 and 306: Page Splits — A First Look All of
- Page 307 and 308: You may hear lots of bad things abo
- Page 309 and 310: Navigating the Tree Figure 9-4 As I
- Page 311 and 312: there was no link between the data.
- Page 313 and 314: Root Non-Leaf Level Leaf Level Figu
- Page 315 and 316: The CREATE INDEX Statement The CREA
- Page 317 and 318: FILLFACTOR When SQL Server first cr
- Page 319 and 320: works only if tempdb is on a separa
- Page 321 and 322: Secondary XML Indexes Chapter 9: SQ
- Page 323 and 324: occur, and that one or more non-lea
- Page 325 and 326: isn’t room on the page, SQL Serve
- Page 327 and 328: more administrator oriented and usu
- Page 329 and 330: The Database Engine Tuning Advisor
- Page 331 and 332: The output is far more self-describ
- Page 333 and 334: We use a FILLFACTOR when we need to
- Page 335: Chapter 9: SQL Server Storage and I
- Page 338 and 339: Chapter 10: Views The preceding syn
- Page 340 and 341: Chapter 10: Views 302 columns to a
- Page 342 and 343: Chapter 10: Views Try It Out Using
- Page 346 and 347: Chapter 10: Views AW00000676 43659
- Page 348 and 349: Chapter 10: Views NULL values will
- Page 350 and 351: Chapter 10: Views Editing V iews wi
- Page 352 and 353: Chapter 10: Views 314 There are fou
- Page 354 and 355: Chapter 10: Views Editing Views in
- Page 356 and 357: Chapter 10: Views 318 In addition,
- Page 358 and 359: Chapter 10: Views 320 from the firs
- Page 360 and 361: Chapter 10: Views You can get the y
- Page 363 and 364: 11 Writing Scripts and Batches Whet
- Page 365 and 366: Next we have a DECLARE statement to
- Page 367 and 368: I’m not going to pick any bones a
- Page 369 and 370: Using @@IDENTITY @@IDENTITY is one
- Page 371 and 372: How It Works What we’re doing in
- Page 373 and 374: DECLARE @RowCount int; --Notice the
- Page 375 and 376: When the editing tool encounters a
- Page 377 and 378: When you think about it, this seems
- Page 379 and 380: So, let’s try a quick query direc
- Page 381 and 382: We now have our text file source fo
- Page 383 and 384: Let’s build an example in the Adv
- Page 385 and 386: DECLARE @InVar varchar(50); DECLARE
- Page 387 and 388: -- This won’t work DECLARE @Numbe
- Page 389 and 390: -- Now we’re run our conditional
- Page 391 and 392: Out of the condition from inner con
- Page 393 and 394: A Simple CASE A simple CASE takes a
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,