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 Try It Out Using a View to Filter Data 304 With these two things in mind, let’s create a new view by changing our old view around just a little bit: CREATE VIEW CurrentEmployees_vw AS SELECT EmployeeID, FirstName, MiddleInitial, LastName, Title, HireDate, ManagerEmpID, Department FROM Employees WHERE TerminationDate IS NULL; In addition to the name change and the WHERE clause we’ve added, note that we’ve also eliminated the TerminationDate column from the SELECT list. Let’s test how this works a little bit by running a straight SELECT statement against our Employees table and limiting our SELECT list to the things that we care about: SELECT EmployeeID, FirstName, LastName, TerminationDate FROM Employees; This returns a few columns from all the rows in the entire table: EmployeeID FirstName LastName TerminationDate ------------ -------------- ------------ ------------------------- 1 Joe Dokey NULL 2 Peter Principle NULL 3 Steve Smith 1997-01-31 4 Howard Kilroy NULL 5 Mary Contrary 1998-06-15 6 Billy Bob NULL (6 row(s) affected) Now let’s check out our view: SELECT EmployeeID, FirstName, LastName FROM CurrentEmployees_vw;
Our result set has become a bit smaller: EmployeeID FirstName LastName ------------- ---------- ---------------- 1 Joe Dokey 2 Peter Principle 4 Howard Kilroy 6 Billy Bob (4 row(s) affected) A few people are missing versus our first select — just the way we wanted it. How It Works As we’ve discussed before, the view really is just a SELECT statement that’s been hidden from the user so that they can ignore what the SELECT statement says, and instead just consider the results it produces just as if it were a table — you can liken this to the derived tables we discussed back in Chapter 7. Because our data was filtered down before we referenced the view by name, our query doesn’t even need to consider that data (the view has done that for us). More Complex V iews Even though I use the term “complex” here — don’t let that scare you. The toughest thing in views is still, for the most part, simpler than most other things in SQL. What we’re doing with more complex views is really just adding joins, summarization, and perhaps some column renaming. Perhaps one of the most common uses of views is to flatten data — that is, the removal of complexity that we outlined at the beginning of the chapter. Imagine that we are providing a view for management to make it easier to check on sales information. No offense to managers who are reading this book, but managers who write their own complex queries are still a rather rare breed — even in the information age. For an example, let’s briefly go back to using the AdventureWorks2008 database. Our manager would like to be able to do simple queries that will tell him or her what orders have been placed for what parts and which account number was used to place the order. So, we create a view that they can perform very simple queries on — remember that we are creating this one in AdventureWorks2008: USE AdventureWorks2008 GO CREATE VIEW CustomerOrders_vw AS SELECT sc.AccountNumber, Chapter 10: Views 305
- Page 291 and 292: Chapter 8: Being Normal: Normalizat
- 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 344 and 345: Chapter 10: Views 306 soh.SalesOrde
- 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
Our result set has become a bit smaller:<br />
EmployeeID FirstName LastName<br />
------------- ---------- ----------------<br />
1 Joe Dokey<br />
2 Peter Principle<br />
4 Howard Kilroy<br />
6 Billy Bob<br />
(4 row(s) affected)<br />
A few people are missing versus our first select — just the way we wanted it.<br />
How It Works<br />
As we’ve discussed before, the view really is just a SELECT statement that’s been hidden from the user<br />
so that they can ignore what the SELECT statement says, and instead just consider the results it produces<br />
just as if it were a table — you can liken this to the derived tables we discussed back in Chapter 7.<br />
Because our data was filtered down before we referenced the view by name, our query doesn’t even<br />
need to consider that data (the view has done that for us).<br />
More Complex V iews<br />
Even though I use the term “complex” here — don’t let that scare you. The toughest thing in views is<br />
still, for the most part, simpler than most other things in <strong>SQL</strong>.<br />
What we’re doing with more complex views is really just adding joins, summarization, and perhaps<br />
some column renaming.<br />
Perhaps one of the most common uses of views is to flatten data — that is, the removal of complexity<br />
that we outlined at the beginning of the chapter. Imagine that we are providing a view for management<br />
to make it easier to check on sales information. No offense to managers who are reading this book, but<br />
managers who write their own complex queries are still a rather rare breed — even in the information age.<br />
For an example, let’s briefly go back to using the AdventureWorks<strong>2008</strong> database. Our manager would<br />
like to be able to do simple queries that will tell him or her what orders have been placed for what parts<br />
and which account number was used to place the order. So, we create a view that they can perform very<br />
simple queries on — remember that we are creating this one in AdventureWorks<strong>2008</strong>:<br />
USE AdventureWorks<strong>2008</strong><br />
GO<br />
CREATE VIEW CustomerOrders_vw<br />
AS<br />
SELECT sc.AccountNumber,<br />
Chapter 10: Views<br />
305