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.

DECLARE @RowCount int; --Notice the single @ sign<br />

SELECT * FROM Person.Person;<br />

SELECT @RowCount = @@ROWCOUNT;<br />

This again shows us all the rows, but notice the new line that we got back:<br />

The value of @@ROWCOUNT was 19972<br />

We’ll take a look at ways this might be useful when we look at stored procedures later in the book. For<br />

now, just realize that this provides us with a way to learn something about what a statement did, and it’s<br />

not limited to use SELECT statements — UPDATE, INSERT, and DELETE also set this value.<br />

Batches<br />

If you look through the example, you might notice that, much as I did with @@IDENTITY,<br />

I chose to move the value off to a holding variable. @@ROWCOUNT will be reset with a<br />

new value the very next statement, so, if you’re going to be doing multiple activities<br />

with the @@ROWCOUNT value, you should move it into a safe keeping area.<br />

A batch is a grouping of T-<strong>SQL</strong> statements into one logical unit. All of the statements within a batch are<br />

combined into one execution plan, so all statements are parsed together and must pass a validation of<br />

the syntax or none of the statements will execute. Note, however, that this does not prevent runtime errors<br />

from happening. In the event of a runtime error, any statement that has been executed prior to the runtime<br />

error will still be in effect. To summarize, if a statement fails at parse-time, then nothing runs. If a statement<br />

fails at runtime, then all statements until the statement that generated the error have already run.<br />

All the scripts we have run up to this point are made up of one batch each. Even the script we’ve been<br />

analyzing so far this in chapter makes up just one batch. To separate a script into multiple batches, we<br />

make use of the GO statement. The GO statement:<br />

❑ Must be on its own line (nothing other than a comment can be on the same line); there is an<br />

exception to this discussed shortly, but think of a GO as needing to be on a line to itself.<br />

❑ Causes all statements since the beginning of the script or the last GO statement (whichever is closer)<br />

to be compiled into one execution plan and sent to the server independently of any other batches.<br />

❑ Is not a T-<strong>SQL</strong> command, but, rather, a command recognized by the various <strong>SQL</strong> <strong>Server</strong> command<br />

utilities (sqlcmd and the Query window in the Management Studio).<br />

A Line to Itself<br />

The GO command should stand alone on its own line. <strong>Tech</strong>nically, you can start a new batch on the same<br />

line after the GO command, but you’ll find this puts a serious damper on readability. T-<strong>SQL</strong> statements<br />

cannot precede the GO statement, or the GO statement will often be misinterpreted and cause either a<br />

parsing error or some other unexpected result. For example, if I use a GO statement after a WHERE clause:<br />

SELECT * FROM Person.Person WHERE ContactID = 1 GO<br />

Chapter 11: Writing Scripts and Batches<br />

335

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

Saved successfully!

Ooh no, something went wrong!