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

Try It Out Using a View to Filter Data<br />

304<br />

With these two things in mind, let’s create a new view by changing our old view around just a little bit:<br />

CREATE VIEW CurrentEmployees_vw<br />

AS<br />

SELECT EmployeeID,<br />

FirstName,<br />

MiddleInitial,<br />

LastName,<br />

Title,<br />

HireDate,<br />

ManagerEmpID,<br />

Department<br />

FROM Employees<br />

WHERE TerminationDate IS NULL;<br />

In addition to the name change and the WHERE clause we’ve added, note that we’ve also eliminated the<br />

TerminationDate column from the SELECT list.<br />

Let’s test how this works a little bit by running a straight SELECT statement against our Employees table<br />

and limiting our SELECT list to the things that we care about:<br />

SELECT EmployeeID,<br />

FirstName,<br />

LastName,<br />

TerminationDate<br />

FROM Employees;<br />

This returns a few columns from all the rows in the entire table:<br />

EmployeeID FirstName LastName TerminationDate<br />

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

1 Joe Dokey NULL<br />

2 Peter Principle NULL<br />

3 Steve Smith 1997-01-31<br />

4 Howard Kilroy NULL<br />

5 Mary Contrary 1998-06-15<br />

6 Billy Bob NULL<br />

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

Now let’s check out our view:<br />

SELECT EmployeeID,<br />

FirstName,<br />

LastName<br />

FROM CurrentEmployees_vw;

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

Saved successfully!

Ooh no, something went wrong!