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.

When the editing tool encounters a GO statement, it sees it as a flag to terminate that batch, package it<br />

up, and send it as a single unit to the server — without including the GO. That’s right, the server itself has<br />

absolutely no idea what GO is supposed to mean.<br />

If you try to execute a GO command in a pass-through query using ODBC, OLE DB, ADO, ADO.NET,<br />

SqlNativeClient, or any other access method, you’ll get an error message back from the server. The GO is merely<br />

an indicator to the tool that it is time to end the current batch, and time, if appropriate, to start a new one.<br />

Errors in Batches<br />

Errors in batches fall into two categories:<br />

❑ Syntax errors<br />

❑ Runtime errors<br />

If the query parser finds a syntax error, processing of that batch is cancelled immediately. Since syntax<br />

checking happens before the batch is compiled or executed, a failure during the syntax check means<br />

none of the batch will be executed — regardless of the position of the syntax error within the batch.<br />

Runtime errors work quite a bit differently. Any statement that has already executed before the runtime<br />

error was encountered is already done, so anything that statement did will remain intact unless it is part<br />

of an uncommitted transaction. (Transactions are covered in Chapter 14, but the relevance here is that<br />

they imply an all or nothing situation.) What happens beyond the point of the runtime error depends on<br />

the nature of the error. Generally speaking, runtime errors terminate execution of the batch from the<br />

point where the error occurred to the end of the batch. Some runtime errors, such as a referentialintegrity<br />

violation, will only prevent the offending statement from executing — all other statements in<br />

the batch will still be executed. This later scenario is why error checking is so important — we will cover<br />

error checking in full in our chapter on stored procedures (Chapter 12).<br />

When to Use Batches<br />

Batches have several purposes, but they all have one thing in common — they are used when something<br />

has to happen either before or separately from everything else in your script.<br />

Statements That Require Their Own Batch<br />

There are several commands that absolutely must be part of their own batch. These include:<br />

❑ CREATE DEFAULT<br />

❑ CREATE PROCEDURE<br />

❑ CREATE RULE<br />

❑ CREATE TRIGGER<br />

❑ CREATE VIEW<br />

Chapter 11: Writing Scripts and Batches<br />

If you want to combine any of these statements with other statements in a single script, then you will<br />

need to break them up into their own batch by using a GO statement.<br />

337

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

Saved successfully!

Ooh no, something went wrong!