GO is the default batch separator keyword for T-SQL. It signals the end of a batch of Transact-SQL statements to the SQL Server utilities.
Actually, GO is not a Transact-SQL statement. It is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor. The SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of SQL Server. So, the current batch of statements is composed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO.
GO must be the only keyword in the line. GO is a utility command that requires no permissions. It can be executed by any user.
Syntax for GO is :
GO [count]
Here, the COUNT is a positive integer. The batch preceding GO will execute the specified number of times.
Example:
PRINT ‘Subhashish’;
GO 5
Output:
Beginning execution loop Subhashish Subhashish Subhashish Subhashish
Subhashish
Batch execution completed 5 times.
The scope of local (user-defined) variables is limited to a batch, and cannot be referenced after a GO command. The SQL Server utilities never send a GO command to the server. As soon as we terminate a batch i.e. just after a GO command, the batch kills all local variables, temporary variables, and cursors created by that batch.
Examples are given below –
USE SalesOrder; GO DECLARE @text VARCHAR(75); SELECT @text = ‘Sales Order’;
GO
Here, the local variable ‘text’ is not at all valid after this GO ends the batch.
USE TEST; GO PRINT @text;
GO
Since the ‘text’ has not been declared, it will generate an error.
Any execution of a stored procedure after the first statement in a batch must include the EXECUTE keyword.
Example:
USE SalesOrder; GO SELECT @@VERSION; sp_who;
GO
It will show Incorrect syntax error.
USE SalesOrder; GO SELECT @@VERSION; EXECUTE sp_who;
GO
This will execute successfully.
References: http://msdn.microsoft.com/en-us/library/ms188037.aspx