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 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

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

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

Saved successfully!

Ooh no, something went wrong!