Interview Question of the Week #057 – What is GO Statement in SQL SERVER?

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.

Interview Question of the Week #057 - What is GO Statement in SQL SERVER? gostatement

The GO Statement must be written in new line as it is not a T-SQL command. T-SQL statement cannot occupy the same line as GO. GO statement can contain comments.

Following is example for SQL SERVER for database Adventureworks.

USE AdventureWorks2014;
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

Here is question to you – did you know about this feature before you read it here?

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL Scripts, SQL Server
Previous Post
Interview Question of the Week #056 – How to fix Installation Failure – Rule “Setup account privileges” Failed in SQL Server
Next Post
Interview Question of the Week #058 – What is the Difference Among DECIMAL, FLOAT and NUMERIC Datatype?

Related Posts

1 Comment. Leave new

  • I wonder how many younger (under 45 years of age) ever worked with batch data processing. When the world used punch cards, we used to say that IBM’s OS/JCL (job control language) would make perfect sense to you after only a year of daily use :) Among other design flaws, all the commands had no spaces; a space separated an optional comment to the right side on each line (card). The simple GO from Sybase was wonderful compared to JCL!

    Reply

Leave a Reply