tags:

views:

370

answers:

5

SQL Server Management Studio always inserts a GO command when I create a query using the right click "Script As" menu. Why? What does GO actually do?

I was curious about this so I looked it up. I'll provide the information I found in an answer in keeping with the SO FAQ.

+5  A: 

The GO command isn't a Transact-SQL statement, but a special command recognized by several MS utilities including SQL Server Management Studio code editor.

The GO command is used to group SQL commands into batches which are sent to the server together. The commands included in the batch, that is, the set of commands since the last GO command or the start of the session, must be logically consistent. For example, you can't define a variable in one batch and then use it in another since the scope of the variable is limited to the batch in which it's defined.

For more information, see http://msdn.microsoft.com/en-us/library/ms188037.aspx.

tvanfosson
+2  A: 

GO is not a SQL keyword.

It's a batch separator used by client tools (like SSMS) to break the entire script up into batches

Answered before several times... example 1

gbn
In my defense I did look through the suggested duplicates before I submitted the question -- and your example didn't show up, nor is it really a duplicate, though the answer is applicable.
tvanfosson
It is hard to search for "GO" here :-)
gbn
+10  A: 

It is a batch terminator, you can however change it to whatever you want alt text

SQLMenace
+1 I didn't know that.
tvanfosson
+1 Could have some fun with this...
gbn
gbn make it SELECT and look at what happens :-)
SQLMenace
A: 
Use herDatabase
GO ; 

says to execute instructions above it. my Default databae is myDatabase so instead of using myDatabase GO and makes current query to use herDatabase

TonyP
+1  A: 

Since Management Studio 2005 it seems that you can use GO with an int parameter, like:

INSERT INTO mytable DEFAULT VALUES
GO 10

The above will insert 10 rows into mytable. Generally speaking, GO will execute the related sql commands n times.

MicSim
+1 that's very useful!
Martin Smith