SQL Server – SQL Utility Command GO

SQL Utility Command GO

SQL Utility Command GO

GO is not a Transact-SQL statement, even it used in the Transact-SQL. It is a SQL Utility command.

 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.

 A Transact-SQL statement cannot occupy the same line as a GO command. It must be used in new line. However, the line can contain comments.

Example

USE TEST_DB
GO

DECLARE @MyMsg VARCHAR(50)
SELECT @MyMsg = 'Hello, This is www.varindersandhu.in'
GO -- @MyMsg is not valid after this GO ends the batch.

-- Yields an error because @MyMsg not declared in this batch.
Print @MyMsg
GO

Note : GO is a utility command that requires no permissions. It can be executed by any user.