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 11: Writing Scripts and Batches<br />

the parser becomes somewhat confused:<br />

Msg 102, Level 15, State 1, Line 1<br />

Incorrect syntax near ‘GO’.<br />

Each Batch Is Sent to the <strong>Server</strong> Separately<br />

Because each batch is processed independently, an error in one batch does not prevent another batch<br />

from running. To illustrate, take a look at some code:<br />

USE AdventureWorks<strong>2008</strong>;<br />

DECLARE @MyVarchar varchar(50); --This DECLARE only lasts for this batch!<br />

SELECT @MyVarchar = ‘Honey, I’’m home...’;<br />

PRINT ‘Done with first Batch...’;<br />

GO<br />

PRINT @MyVarchar; --This generates an error since @MyVarchar<br />

--isn’t declared in this batch<br />

PRINT ‘Done with second Batch’;<br />

GO<br />

PRINT ‘Done with third batch’; -- Notice that this still gets executed<br />

-- even after the error<br />

GO<br />

If there were any dependencies between these batches, then either everything would fail — or, at the<br />

very least, everything after the point of error would fail — but it doesn’t. Look at the results if you run<br />

the previous script:<br />

Done with first Batch...<br />

Msg 137, Level 15, State 2, Line 2<br />

Must declare the scalar variable “@MyVarchar”.<br />

Done with third batch<br />

Again, each batch is completely autonomous in terms of runtime issues. Keep in mind, however, that<br />

you can build in dependencies in the sense that one batch may try to perform work that depends on the<br />

first batch being complete — we’ll see some of this in the next section when I talk about what can and<br />

can’t span batches.<br />

GO Is Not a T-<strong>SQL</strong> Command<br />

336<br />

Thinking that GO is a T-<strong>SQL</strong> command is a common mistake. GO is a command that is only recognized by<br />

the editing tools (Management Studio, sqlcmd). If you use a third-party tool, then it may or may not<br />

support the GO command, but most that claim <strong>SQL</strong> <strong>Server</strong> support will.

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

Saved successfully!

Ooh no, something went wrong!