Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 4: JOINs JOIN Sales.SpecialOfferProduct ssop ON sso.SpecialOfferID = ssop.SpecialOfferID WHERE sso.SpecialOfferID != 1 Again, we just lose the words LEFT OUTER JOIN and replace the ON operator with a WHERE clause, or, in this case, add the ON condition to the existing WHERE clause: SELECT sso.SpecialOfferID, Description, DiscountPct, ProductID FROM Sales.SpecialOffer sso, Sales.SpecialOfferProduct ssop WHERE sso.SpecialOfferID *= ssop.SpecialOfferID AND sso.SpecialOfferID != 1 If you were to run this (I’d recommend against doing the change in compatibility level that would be required, but I want you to know that this kind of code is out there), we would get back the same results we did in our original OUTER JOIN query. A RIGHT JOIN would function in much the same way. An Alternative CROSS JOIN 106 This is far and away the easiest of the bunch. To create a CROSS JOIN using the old syntax, you just do nothing. That is, you don’t put anything in the WHERE clause of the FROM: TableA.ColumnA = TableB.ColumnA. So, for an ultra quick example, let’s take the first example from the CROSS JOIN section earlier in the chapter. The ANSI syntax looked like this: USE Chapter4DB SELECT v.VendorName, a.Address FROM Vendors v CROSS JOIN Address a To convert it to the old syntax, we just strip out the CROSS JOIN keywords and add a comma: USE Chapter4DB SELECT v.VendorName, a.Address FROM Vendors v, Address a As with the other examples in this section, we get back the same results that we got with the ANSI syntax: VendorName Address --------------------------- -------------------- Don’s Database Design Shop 1234 Anywhere Don’s Database Design Shop 567 Main St. Don’s Database Design Shop 999 1st St. Don’s Database Design Shop 1212 Smith Ave Don’s Database Design Shop 364 Westin Dave’s Data 1234 Anywhere Dave’s Data 567 Main St. Dave’s Data 999 1st St.

Dave’s Data 1212 Smith Ave Dave’s Data 364 Westin The SQL Sequel 1234 Anywhere The SQL Sequel 567 Main St. The SQL Sequel 999 1st St. The SQL Sequel 1212 Smith Ave The SQL Sequel 364 Westin (15 row(s) affected) This is supported across all versions and across most of the database management systems. The UNION OK, enough with all the “old syntax” versus “new syntax” stuff. Now we’re into something that’s the same regardless of what other join syntax you prefer — the UNION operator. UNION is a special operator we can use to cause two or more queries to generate one result set. A UNION isn’t really a JOIN, like the previous options we’ve been looking at — instead it’s more of an appending of the data from one query right onto the end of another query (functionally, it works a little differently than this, but this is the easiest way to look at the concept). Where a JOIN combined information horizontally (adding more columns), a UNION combines data vertically (adding more rows), as illustrated in Figure 4-1. When dealing with queries that use a UNION, there are just a few key points: Chapter 4: JOINs ❑ All the UNIONed queries must have the same number of columns in the SELECT list. If your first query has three columns in the SELECT list, then the second (and any subsequent queries being UNIONed) must also have three columns. If the first has five, then the second must have five, too. Regardless of how many columns are in the first query, there must be the same number in the subsequent query(s). ❑ The headings returned for the combined result set will be taken only from the first of the queries. If your first query has a SELECT list that looks like SELECT Col1, Col2 AS Second, Col3 FROM..., then regardless of how your columns are named or aliased in the subsequent queries, the headings on the columns returned from the UNION will be Col1, Second, and Col3 respectively. ❑ The data types of each column in a query must be implicitly compatible with the data type in the same relative column in the other queries. Note that I’m not saying they have to be the same data type — they just have to be implicitly convertible (a conversion table that shows implicit versus explicit conversions can be found in Figure 1-3 of Chapter 1). If the second column in the first query were of type char(20), then it would be fine if the second column in the second query were varchar(50). However, because things are based on the first query, any rows longer than 20 would be truncated for data from the second result set. ❑ Unlike non-UNION queries, the default return option for UNIONs is DISTINCT rather than ALL. This can really be confusing to people. In your other queries, all rows were returned regardless of whether they were duplicated with another row or not, but the results of a UNION do not work that way. Unless you use the ALL keyword in your query, only one of any repeating rows will be returned. 107

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!