SQL SERVER – Explanation SQL Command GO

GO is not a Transact-SQL statement; it is often used in T-SQL code. Go causes all statements from the beginning of the script or the last GO statement (whichever is closer) to be compiled into one execution plan and sent to the server independent of any other batches. 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. 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 Statement must be written in new line as it is not T-SQL command. T-SQL statement can not occupy the same line as GO. GO statement can contain comments.

Following is example for SQL SERVER 2005 for database Adventureworks.
USE AdventureWorks;
GO
DECLARE @MyMsg VARCHAR(50)
SELECT @MyMsg = 'Hello, World.'
GO ---- @MyMsg is not valid after this GO ends the batch.

—- Yields an error because @MyMsg not declared in this batch.
PRINT @MyMsg
GO
Reference : Pinal Dave (https://darkslategrey-bat-805937.hostingersite.com) , Example BOL

SQL Scripts, SQL Utility
Previous Post
SQL SERVER – Download Microsoft SQL Server 2005 System Views Map
Next Post
SQL SERVER – Fix : Error : Msg 6263, Level 16, State 1, Line 2 Enabling SQL Server 2005 for CLR Support

Related Posts

Leave a Reply