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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4: JOINs<br />

86<br />

Uh, oh — this is a problem. You get an error back:<br />

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

Ambiguous column name ‘BusinessEntityID’.<br />

Why did JobTitle work and BusinessEntityID not work? For just the reason <strong>SQL</strong> <strong>Server</strong> has<br />

indicated — our column name is ambiguous. While JobTitle exists only in the Employee table,<br />

BusinessEntityID appears in both tables. <strong>SQL</strong> <strong>Server</strong> has no way of knowing which one we want. All<br />

the instances where we have returned BusinessEntityID up to this point have been resolvable: that is,<br />

<strong>SQL</strong> <strong>Server</strong> could figure out which column was which. In the first query (where we used a plain * operator),<br />

we asked <strong>SQL</strong> <strong>Server</strong> to return everything — that would include both BusinessEntityID columns,<br />

so no name resolution was necessary. In our second example (where we qualified the * to be only for<br />

BusinessEntity), we again said nothing specifically about which BusinessEntityID column to<br />

use — instead, we said pull everything from the Contact table and BusinessEntityID just happened<br />

to be in that list. JobTitle was resolvable because there was only one JobTitle column, so that was<br />

the one we wanted.<br />

When we want to refer to a column where the column name exists more than once in our JOIN result,<br />

we must fully qualify the column name. We can do this in one of two ways:<br />

❑ Provide the name of the table that the desired column is from, followed by a period and the<br />

column name (Table.ColumnName)<br />

❑ Alias the tables, and provide that alias, followed by a period and the column name<br />

(Alias.ColumnName), as shown in the previous example<br />

The task of providing the names is straightforward enough — we’ve already seen how that works with<br />

the qualified * operator, but let’s try our BusinessEntityID query again with a qualified column name:<br />

SELECT Person. BusinessEntity.*, HumanResources.Employee. BusinessEntityID<br />

FROM Person. BusinessEntity<br />

INNER JOIN HumanResources.Employee<br />

ON Person. BusinessEntity. BusinessEntityID<br />

= HumanResources.Employee. BusinessEntityID<br />

Now things are working again and the BusinessEntityID from the Employee table is added to the far<br />

right-hand side of the result set.<br />

Aliasing the table is only slightly trickier, but can cut down on the wordiness and help the readability of<br />

your query. It works almost exactly the same as aliasing a column in the simple SELECTs that we did in<br />

the previous chapter — right after the name of the table, we simply state the alias we want to use to refer<br />

to that table. Note that, just as with column aliasing, we can use the AS keyword (but for some strange<br />

reason, this hasn’t caught on as much in practice):<br />

SELECT pbe.*, hre.BusinessEntityID<br />

FROM Person.BusinessEntity pbe<br />

INNER JOIN HumanResources.Employee hre<br />

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

Run this code and you’ll see that we receive the exact same results as in the previous query.

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

Saved successfully!

Ooh no, something went wrong!