SQL Keywords

SQL GO

What is the SQL GO command?

GO signals the end of a batch to SQL Server-family client utilities, causing the preceding T-SQL statements to execute as one unit.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL GO: Supported: Microsoft SQL Server, Azure SQL Database, Azure Synapse Analytics, SQL Server on Linux/Windows, Sybase ASE. Not Supported: PostgreSQL, MySQL, Oracle, SQLite, Snowflake, BigQuery.

SQL GO Full Explanation

GO is not a T-SQL command processed by the SQL Server engine. Instead, it is a batch separator that client tools such as SQL Server Management Studio (SSMS), sqlcmd, and Azure Data Studio interpret. When the tool encounters GO, it sends all statements collected since the previous GO (or start of the script) to the server as a single batch. Each batch is compiled separately, so variables declared after a GO are out of scope for subsequent batches, and certain statements (CREATE PROCEDURE, CREATE VIEW, etc.) must be the first in their batch. An optional integer can follow GO (e.g., GO 5). The tool will resend the preceding batch that many times. GO cannot appear inside strings, multi-statement functions, or stored-procedure bodies. In those contexts use semicolons or BEGIN…END blocks instead. Because GO is client-side, scripts executed through APIs (ADO.NET, JDBC, etc.) must omit it or split the text manually.

SQL GO Syntax

-- basic
GO

-- repeat preceding batch n times
GO <count>

SQL GO Parameters

  • count (INT) - Optional. Positive integer that repeats execution of the immediately preceding batch the specified number of times.

Example Queries Using SQL GO

-- Example 1: separate two batches
DECLARE @x INT = 1;
SELECT @x AS FirstBatch;
GO
-- @x is now out of scope. Next line would fail if uncommented.
-- SELECT @x;

-- Example 2: repeat a batch
PRINT 'Hello';
GO 3

Expected Output Using SQL GO

  • Example 1: The first batch returns a single row with value 1
  • The second batch runs independently; any reference to @x would raise an error
  • Example 2: The string "Hello" is printed three times because the batch is executed three times

Use Cases with SQL GO

  • Splitting large scripts into logical sections
  • Isolating variable scope to avoid naming collisions
  • Ensuring DDL statements like CREATE PROCEDURE start a new batch
  • Repeating a batch multiple times for test data generation

Common Mistakes with SQL GO

  • Thinking GO is part of T-SQL and using it in dynamic SQL or API calls
  • Referencing variables across a GO boundary
  • Placing GO inside stored-procedure bodies or string literals
  • Forgetting that GO 0 or negative numbers raise errors

Related Topics

Batches, Semicolon statement terminator, BEGIN…END, sqlcmd, SSMS, Variable scope

First Introduced In

Sybase SQL Server 4.x (late 1980s)

Frequently Asked Questions

Is GO part of T-SQL?

No. GO is interpreted by client utilities like SSMS and sqlcmd and never reaches the SQL Server engine.

Do I need GO after every statement?

Only when you want to start a new batch. Use semicolons for normal statement termination.

Why does GO break variable scope?

Variables live only for the duration of the batch. A GO ends the batch, so variables declared before it are discarded.

How can I loop quickly with GO?

Add an integer after GO, for example `GO 5`, to resend the preceding batch five times.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!