Forum Moderators: open

Message Too Old, No Replies

Purpose of "GO" keyword

         

ramoneguru

5:57 pm on Jun 6, 2006 (gmt 0)

10+ Year Member



I know that the "GO" keyword in SQL Server groups a set of commands into a batch. Now, I'm maintianing a db at the moment and it seems this person is using this keyword a lot (pretty much after every SQL statement) and it looks like this:

SELECT <expression>
<stuff>
GO

SELECT <expression>
<stuff>
GO

And so on. When should/shouldn't I be using the GO statement. Do I use it if I'm doing multiple queries (more than 10) or are there mission critical times when it needs to be used? I'm lost, little help.
--Nick

syber

1:05 pm on Jun 8, 2006 (gmt 0)

10+ Year Member



There are only 5 commands that require a GO statement: CREATE DEFAULT, CREATE PROCEDURE, CREATE RULE, CREATE TRIGGER, and CREATE VIEW.

Multiple line comments ( /* */) cannot include a GO statement

A runtime error such as an arithmetic overflow will stop the remainder of the statements from executing.

Outside of the above, you would use a GO to:

1. determine the scope of local variables
( a local variable declared in one batch
cannot be read in another batch)

2. solve timing problems
(A table cannot be altered and then the
new columns referenced in the same batch)

3. group multiple SELECT statements together
for faster processing (10 SELECT statements
in a batch will process faster than 10
batches of one SELECT each)

Your example of a GO after every statement would create many more batches than necessary.