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

Interview Question of the Week #057 looks tiny but can trip people up: what does the GO statement do in SQL Server? The surprise is that GO is not part of Transact-SQL at all. The server never sees it.

A railway semaphore signal arm dropped to the clear position, the track running away into morning mist.

What GO really is

GO is a command for the client tool. SQL Server Management Studio, sqlcmd and osql read your script, and when they reach a GO on its own line, they take everything above it (back to the previous GO, or to the start of the script) and send that chunk to the server as one batch. The server compiles that batch into one execution plan and runs it, knowing nothing about the batches that came before or after.

Because GO is not a T-SQL command, it follows a few rules of its own:

  • It must sit on its own line. You cannot put a SELECT or any other statement on the same line as GO.
  • A comment after GO on the same line is fine, as you will see in the script below.
  • You can add a number after it, such as GO 5, and the tool runs that batch five times.
  • Programs that send T-SQL to the server through a data access API, not through these tools, get a syntax error on GO.

Why your variable disappears

A local variable lives only inside the batch that declares it. Here is a small script for the AdventureWorks sample database. The variable is declared and given a value, and then GO closes the batch.


USE AdventureWorks2014;
GO
DECLARE @MyMsg VARCHAR(50)
SELECT @MyMsg = 'Hello, World.'
GO ---- @MyMsg is not valid after this GO ends the batch.

Next, a separate batch tries to print that variable:

A folded starter's flag resting on a post beside an empty running track.


PRINT @MyMsg
GO

This one fails with the message Must declare the scalar variable “@MyMsg”. The variable was gone the moment the first batch finished, so as far as the second batch is concerned, it never existed.

This is also why deployment scripts are full of GO. Statements such as CREATE PROCEDURE, CREATE VIEW, CREATE FUNCTION and CREATE TRIGGER must be the first statement in their batch, so scripts put a GO right before each one.

One last tip: GO is only the default word. Management Studio lets you change the batch separator in its query execution options, though I would leave it alone unless you enjoy confusing your teammates.

So here is my question back to you: did you know GO was a tool command and not a T-SQL statement before today?

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.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

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.