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

Every time I teach a <strong>SQL</strong> class, I get asked the same question about CROSS JOINs: “Why in the world<br />

would you use something like this?” I’m told there are scientific uses for it — this makes sense to me<br />

since I know there are a number of high-level mathematical functions that make use of Cartesian products.<br />

I presume that you could read a large number of samples into table structures, and then perform<br />

your CROSS JOIN to create a Cartesian product of your sample. There is, however, a much more frequently<br />

occurring use for CROSS JOINs — the creation of test data.<br />

When you are building up a database, that database is quite often part of a larger-scale system that will<br />

need substantial testing. A recurring problem in testing of large-scale systems is the creation of large<br />

amounts of test data. By using a CROSS JOIN, you can do smaller amounts of data entry to create your<br />

test data in two or more tables, and then perform a CROSS JOIN against the tables to produce a much<br />

larger set of test data. You have a great example in the previous query — if you needed to match a group<br />

of addresses up with a group of vendors, then that simple query yields 15 records from 8. Of course, the<br />

numbers can become far more dramatic. For example, if we created a table with 50 first names and then<br />

created a table with 250 last names, we could CROSS JOIN them together to create a table with 12,500<br />

unique name combinations. By investing in keying in 300 names, we suddenly get a set of test data with<br />

12,500 names.<br />

Exploring Alter native Syntax for Joins<br />

104<br />

What we’re going to look at in this section is what many people still consider to be the “normal” way of<br />

coding joins. Until <strong>SQL</strong> <strong>Server</strong> 6.5, the alternative syntax we’ll look at here was the only join syntax in<br />

<strong>SQL</strong> <strong>Server</strong>, and what is today called the “standard” way of coding joins wasn’t even an option.<br />

Until now, we have been using the ANSI/ISO syntax for all of our <strong>SQL</strong> statements. I highly recommend<br />

that you use the ANSI method since it has much better portability between systems and is also much<br />

more readable. It is worth noting that the old syntax is actually reasonably well supported across platforms<br />

at the current time.<br />

The primary reason I am covering the old syntax at all is that there is absolutely no doubt that, sooner<br />

or later, you will run into it in legacy code. I don’t want you staring at that code saying, “What the heck<br />

is this?”<br />

That being said, I want to reiterate my strong recommendation that you use the ANSI syntax wherever<br />

possible. Again, it is substantially more readable and <strong>Microsoft</strong> has indicated that they may not continue<br />

to support the old syntax indefinitely. I find it very hard to believe, given the amount of legacy<br />

code out there, that <strong>Microsoft</strong> will dump the old syntax any time soon, but you never know.<br />

Perhaps the biggest reason is that the ANSI syntax is actually more functional. Under old syntax, it<br />

was actually possible to create ambiguous query logic — where there was more than one way to interpret<br />

the query. The new syntax eliminates this problem.<br />

Remember when I compared a JOIN to a WHERE clause earlier in this chapter? Well, there was a reason.<br />

The old syntax expresses all of the JOINs within the WHERE clause.<br />

The old syntax supports all of the JOINs that we’ve done using ANSI with the exception of a FULL<br />

JOIN. If you need to perform a FULL JOIN, I’m afraid you’ll have to stick with the ANSI version.

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

Saved successfully!

Ooh no, something went wrong!