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 4: JOINs<br />

JOIN Sales.SpecialOfferProduct ssop<br />

ON sso.SpecialOfferID = ssop.SpecialOfferID<br />

WHERE sso.SpecialOfferID != 1<br />

Again, we just lose the words LEFT OUTER JOIN and replace the ON operator with a WHERE clause, or, in<br />

this case, add the ON condition to the existing WHERE clause:<br />

SELECT sso.SpecialOfferID, Description, DiscountPct, ProductID<br />

FROM Sales.SpecialOffer sso,<br />

Sales.SpecialOfferProduct ssop<br />

WHERE sso.SpecialOfferID *= ssop.SpecialOfferID<br />

AND sso.SpecialOfferID != 1<br />

If you were to run this (I’d recommend against doing the change in compatibility level that would be<br />

required, but I want you to know that this kind of code is out there), we would get back the same results<br />

we did in our original OUTER JOIN query. A RIGHT JOIN would function in much the same way.<br />

An Alternative CROSS JOIN<br />

106<br />

This is far and away the easiest of the bunch. To create a CROSS JOIN using the old syntax, you just do<br />

nothing. That is, you don’t put anything in the WHERE clause of the FROM: TableA.ColumnA =<br />

TableB.ColumnA.<br />

So, for an ultra quick example, let’s take the first example from the CROSS JOIN section earlier in the<br />

chapter. The ANSI syntax looked like this:<br />

USE Chapter4DB<br />

SELECT v.VendorName, a.Address<br />

FROM Vendors v<br />

CROSS JOIN Address a<br />

To convert it to the old syntax, we just strip out the CROSS JOIN keywords and add a comma:<br />

USE Chapter4DB<br />

SELECT v.VendorName, a.Address<br />

FROM Vendors v, Address a<br />

As with the other examples in this section, we get back the same results that we got with the ANSI syntax:<br />

VendorName Address<br />

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

Don’s Database Design Shop 1234 Anywhere<br />

Don’s Database Design Shop 567 Main St.<br />

Don’s Database Design Shop 999 1st St.<br />

Don’s Database Design Shop 1212 Smith Ave<br />

Don’s Database Design Shop 364 Westin<br />

Dave’s Data 1234 Anywhere<br />

Dave’s Data 567 Main St.<br />

Dave’s Data 999 1st St.

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

Saved successfully!

Ooh no, something went wrong!