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 3: The Foundation Statements of T-<strong>SQL</strong><br />

While T-<strong>SQL</strong> is unique to <strong>SQL</strong> <strong>Server</strong>, the statements you use most of the time are not. T-<strong>SQL</strong> is largely<br />

ANSI/ISO compliant (The standard was originally governed by ANSI, and was later taken over by the<br />

ISO. It was ANSI long enough that people generally still refer to it as ANSI compliance.), which means<br />

that, by and large, it complies with a very wide open standard. What this means to you as a developer is that<br />

much of the <strong>SQL</strong> you’re going to learn in this book is directly transferable to other <strong>SQL</strong>-based database<br />

servers such as Sybase (which long ago used to share the same code base as <strong>SQL</strong> <strong>Server</strong>), Oracle, DB2,<br />

and My<strong>SQL</strong>. Be aware, however, that every RDBMS has different extensions and performance enhancements<br />

that it uses above and beyond the ANSI/ISO standard. I will try to point out the ANSI vs. non-<br />

ANSI ways of doing things where applicable. In some cases, you’ll have a choice to make — performance<br />

versus portability to other RDBMS systems. Most of the time, however, the ANSI way is as fast as any<br />

other option. In such a case, the choice should be clear: Stay ANSI compliant.<br />

Getting Star ted with a Basic SELECT<br />

Statement<br />

If you haven’t used <strong>SQL</strong> before, or don’t feel like you’ve really understood it yet, pay attention here! The<br />

SELECT statement and the structures used within it form the basis of the lion’s share of all the commands<br />

we will perform with <strong>SQL</strong> <strong>Server</strong>. Let’s look at the basic syntax rules for a SELECT statement:<br />

SELECT [ALL|DISTINCT] [TOP () [PERCENT] [WITH TIES]] <br />

[FROM ]<br />

[WHERE ]<br />

[GROUP BY ]<br />

[HAVING ]<br />

[ORDER BY ]<br />

[[FOR XML {RAW|AUTO|EXPLICIT|PATH [()]}[, XMLDATA][, ELEMENTS][, BINARY<br />

base 64]]<br />

[OPTION (, [, ...n])]<br />

Wow — that’s a lot to decipher, so let’s look at the parts.<br />

Note that the parentheses around the TOP expression are, technically speaking, optional. <strong>Microsoft</strong><br />

refers to them as “required,” then points out that a lack of parentheses is actually supported, but for<br />

backward compatibility only. This means that <strong>Microsoft</strong> may pull support for that in a later release, so if<br />

you do not need to support older versions of <strong>SQL</strong> <strong>Server</strong>, I strongly recommend using parentheses to<br />

delimit a TOP expression in your queries.<br />

The SELECT Statement and FROM Clause<br />

44<br />

The verb — in this case a SELECT — is the part of the overall statement that tells <strong>SQL</strong> <strong>Server</strong> what we are<br />

doing. A SELECT indicates that we are merely reading information, as opposed to modifying it. What we<br />

are selecting is identified by an expression or column list immediately following the SELECT. You’ll see<br />

what I mean by this in a moment.<br />

Next, we add in more specifics, such as where we are getting this data. The FROM statement specifies the<br />

name of the table or tables from which we are getting our data. With these, we have enough to create a

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

Saved successfully!

Ooh no, something went wrong!