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

Figure 4-1<br />

As always, let’s take a look at this with an example or two.<br />

Try It Out UNION<br />

108<br />

Select Col1 FROM<br />

Table1<br />

Row1<br />

Row2<br />

Row3<br />

Row4<br />

Row5<br />

Row6<br />

Select Col5 FROM<br />

Table2<br />

RowA<br />

RowB<br />

RowC<br />

RowD<br />

RowE<br />

RowF<br />

First, let’s look at a UNION that has some practical use to it. (It’s something I could see happening in the<br />

real world — albeit not all that often.) For this example, we’re going to assume that AdventureWorks is<br />

moving to a new facility and we want to send out an email to all of our customers and vendors. We want<br />

to return a list of names and email addresses to our address update. We can do this in just one query<br />

with something like this:<br />

USE AdventureWorks<strong>2008</strong><br />

SELECT FirstName + ‘ ‘ + LastName AS Name, pe.EmailAddress<br />

EmailAddress<br />

FROM Person.Person pp<br />

JOIN Person.EmailAddress pe<br />

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

Select Col1 FROM<br />

Table1<br />

UNION<br />

Select Col5 FROM<br />

Table2<br />

Row1<br />

Row2<br />

Row3<br />

Row4<br />

Row5<br />

Row6<br />

RowA<br />

RowB<br />

RowC<br />

RowD<br />

RowE<br />

RowF

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

Saved successfully!

Ooh no, something went wrong!