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.

Be aware that using an alias is an all-or-nothing proposition. Once you decide to alias a table, you must<br />

use that alias in every part of the query. This is on a table-by-table basis, but try running some mixed<br />

code and you’ll see what I mean:<br />

SELECT pbe.*, HumanResources.Employee.BusinessEntityID<br />

FROM Person.BusinessEntity pbe<br />

INNER JOIN HumanResources.Employee hre<br />

ON pbe.BusinessEntityID = hre.BusinessEntityID<br />

This seems like it should run fine, but it will give you an error:<br />

Msg 4104, Level 16, State 1, Line 1<br />

The multi-part identifier “HumanResources.Employee.BusinessEntityID” could not be<br />

bound.<br />

Again, you can mix and match which tables you choose to use aliasing on and which you don’t, but once<br />

you make a decision for a given table, you have to be consistent in how you reference that table.<br />

Think back to those bullet points we saw a few pages earlier; the columns from the first table listed in<br />

the JOIN were the first columns returned. Take a break for a moment and think about why that is, and<br />

what you might be able to do to control it.<br />

<strong>SQL</strong> <strong>Server</strong> always uses a column order that is the best guess it can make at how you want the columns<br />

returned. In our first query we used one global * operator, so <strong>SQL</strong> <strong>Server</strong> didn’t have much to go on. In<br />

that case, it goes on the small amount that it does have — the order of the columns as they exist physically<br />

in the table and the order of tables that you specified in your query. The nice thing is that it is extremely<br />

easy to reorder the columns — we just have to be explicit about it. The simplest way to reorder the columns<br />

would be to change which table is mentioned first, but we can actually mix and match your column order<br />

by simply explicitly stating the columns that we want (even if it is every column), and the order in which<br />

we want them.<br />

Try It Out A Simple JOIN<br />

Let’s try a small query to demonstrate the point:<br />

SELECT pbe.BusinessEntityID, hre.JobTitle, pp.FirstName, pp.LastName<br />

FROM Person.BusinessEntity pbe<br />

INNER JOIN HumanResources.Employee hre<br />

ON pbe.BusinessEntityID = hre.BusinessEntityID<br />

INNER JOIN Person.Person pp<br />

ON pbe.BusinessEntityID = pp.BusinessEntityID<br />

WHERE hre.BusinessEntityID < 4<br />

This yields a pretty simple result set:<br />

BusinessEntityID JobTitle FirstName LastName<br />

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

1 Chief Executive Officer Ken Sánchez<br />

2 Vice President of Engineering Terri Duffy<br />

3 Engineering Manager Roberto Tamburello<br />

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

Chapter 4: JOINs<br />

87

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

Saved successfully!

Ooh no, something went wrong!