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 11: Writing Scripts and Batches the parser becomes somewhat confused: Msg 102, Level 15, State 1, Line 1 Incorrect syntax near ‘GO’. Each Batch Is Sent to the Server Separately Because each batch is processed independently, an error in one batch does not prevent another batch from running. To illustrate, take a look at some code: USE AdventureWorks2008; DECLARE @MyVarchar varchar(50); --This DECLARE only lasts for this batch! SELECT @MyVarchar = ‘Honey, I’’m home...’; PRINT ‘Done with first Batch...’; GO PRINT @MyVarchar; --This generates an error since @MyVarchar --isn’t declared in this batch PRINT ‘Done with second Batch’; GO PRINT ‘Done with third batch’; -- Notice that this still gets executed -- even after the error GO If there were any dependencies between these batches, then either everything would fail — or, at the very least, everything after the point of error would fail — but it doesn’t. Look at the results if you run the previous script: Done with first Batch... Msg 137, Level 15, State 2, Line 2 Must declare the scalar variable “@MyVarchar”. Done with third batch Again, each batch is completely autonomous in terms of runtime issues. Keep in mind, however, that you can build in dependencies in the sense that one batch may try to perform work that depends on the first batch being complete — we’ll see some of this in the next section when I talk about what can and can’t span batches. GO Is Not a T-SQL Command 336 Thinking that GO is a T-SQL command is a common mistake. GO is a command that is only recognized by the editing tools (Management Studio, sqlcmd). If you use a third-party tool, then it may or may not support the GO command, but most that claim SQL Server support will.

When the editing tool encounters a GO statement, it sees it as a flag to terminate that batch, package it up, and send it as a single unit to the server — without including the GO. That’s right, the server itself has absolutely no idea what GO is supposed to mean. If you try to execute a GO command in a pass-through query using ODBC, OLE DB, ADO, ADO.NET, SqlNativeClient, or any other access method, you’ll get an error message back from the server. The GO is merely an indicator to the tool that it is time to end the current batch, and time, if appropriate, to start a new one. Errors in Batches Errors in batches fall into two categories: ❑ Syntax errors ❑ Runtime errors If the query parser finds a syntax error, processing of that batch is cancelled immediately. Since syntax checking happens before the batch is compiled or executed, a failure during the syntax check means none of the batch will be executed — regardless of the position of the syntax error within the batch. Runtime errors work quite a bit differently. Any statement that has already executed before the runtime error was encountered is already done, so anything that statement did will remain intact unless it is part of an uncommitted transaction. (Transactions are covered in Chapter 14, but the relevance here is that they imply an all or nothing situation.) What happens beyond the point of the runtime error depends on the nature of the error. Generally speaking, runtime errors terminate execution of the batch from the point where the error occurred to the end of the batch. Some runtime errors, such as a referentialintegrity violation, will only prevent the offending statement from executing — all other statements in the batch will still be executed. This later scenario is why error checking is so important — we will cover error checking in full in our chapter on stored procedures (Chapter 12). When to Use Batches Batches have several purposes, but they all have one thing in common — they are used when something has to happen either before or separately from everything else in your script. Statements That Require Their Own Batch There are several commands that absolutely must be part of their own batch. These include: ❑ CREATE DEFAULT ❑ CREATE PROCEDURE ❑ CREATE RULE ❑ CREATE TRIGGER ❑ CREATE VIEW Chapter 11: Writing Scripts and Batches If you want to combine any of these statements with other statements in a single script, then you will need to break them up into their own batch by using a GO statement. 337

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!