Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 323 and 324: occur, and that one or more non-lea
- Page 325 and 326: isn’t room on the page, SQL Serve
- Page 327 and 328: more administrator oriented and usu
- Page 329 and 330: The Database Engine Tuning Advisor
- Page 331 and 332: The output is far more self-describ
- Page 333 and 334: We use a FILLFACTOR when we need to
- Page 335: Chapter 9: SQL Server Storage and I
- Page 338 and 339: Chapter 10: Views The preceding syn
- Page 340 and 341: Chapter 10: Views 302 columns to a
- Page 342 and 343: Chapter 10: Views Try It Out Using
- Page 344 and 345: Chapter 10: Views 306 soh.SalesOrde
- Page 346 and 347: Chapter 10: Views AW00000676 43659
- Page 348 and 349: Chapter 10: Views NULL values will
- Page 350 and 351: Chapter 10: Views Editing V iews wi
- Page 352 and 353: Chapter 10: Views 314 There are fou
- Page 354 and 355: Chapter 10: Views Editing Views in
- Page 356 and 357: Chapter 10: Views 318 In addition,
- Page 358 and 359: Chapter 10: Views 320 from the firs
- Page 360 and 361: Chapter 10: Views You can get the y
- Page 363 and 364: 11 Writing Scripts and Batches Whet
- Page 365 and 366: Next we have a DECLARE statement to
- Page 367 and 368: I’m not going to pick any bones a
- Page 369 and 370: Using @@IDENTITY @@IDENTITY is one
- Page 371 and 372: How It Works What we’re doing in
- Page 373: DECLARE @RowCount int; --Notice the
- Page 377 and 378: When you think about it, this seems
- Page 379 and 380: So, let’s try a quick query direc
- Page 381 and 382: We now have our text file source fo
- Page 383 and 384: Let’s build an example in the Adv
- Page 385 and 386: DECLARE @InVar varchar(50); DECLARE
- Page 387 and 388: -- This won’t work DECLARE @Numbe
- Page 389 and 390: -- Now we’re run our conditional
- Page 391 and 392: Out of the condition from inner con
- Page 393 and 394: A Simple CASE A simple CASE takes a
- Page 395 and 396: 3 8 More Than One Apart 2 2 Ends Wi
- Page 397 and 398: Now, I don’t know about you, but
- Page 399 and 400: The WAITFOR statement does exactly
- Page 401 and 402: IF @ErrorNo = 2714 -- Object exists
- Page 403: Chapter 11: Writing Scripts and Bat
- Page 406 and 407: Chapter 12: Stored Procedures Creat
- Page 408 and 409: Chapter 12: Stored Procedures Dropp
- Page 410 and 411: Chapter 12: Stored Procedures Suppl
- Page 412 and 413: Chapter 12: Stored Procedures 374 [
- Page 414 and 415: Chapter 12: Stored Procedures Confi
- Page 416 and 417: Chapter 12: Stored Procedures Now,
- Page 418 and 419: Chapter 12: Stored Procedures SQL S
- Page 420 and 421: Chapter 12: Stored Procedures 382 c
- Page 422 and 423: Chapter 12: Stored Procedures It wo
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.