In SQL Server, GO is not Transact-SQL statements, but signals the end of a batch of Transact-SQL statements to the SQL Server utilities, so in this article, I have mentioned how to use Go in SQL Server with example.

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.

A Transact-SQL statement cannot occupy the same line as a GO command.

So, from the above details we can understand that GO statement is used as a Batch separator in SQL Server and used by client tools (like SSMS) to break the entire script up into batches

A batch is nothing but one or more SQL Server statements sent to the SQL Server engine as one set of statements.

GO Statement can also be used to execute a batch of T-Sql statements multiple times.

Let us understand GO statement with a few examples:

DECLARE @Name NVARCHAR(50) = 'Vikram'
SELECT @Name AS 'Name'
GO -- Go statement seperate batches of command

DECLARE @Name NVARCHAR(50) = 'Vikas'
SELECT @Name AS 'Name'

Output:

go-statement-sql-server

As you can see from the above SQL Query, we have declared 2 @Name but still above query works fine, since, we have seperated Query using "GO" statement, so GO separates the query as batches.

We can take a look at another similar example, in which we will declare a variable before GO statement and then use it after GO statement, it will throw error because GO separates the query.

DECLARE @Name NVARCHAR(50)  
SELECT @Name = 'Vikram'  
GO -- @Name is not valid after this GO ends the batch.  
  
-- Yields an error because @Name not declared in this batch.  
PRINT @Name  
GO  
-- Error message "Must declare the scalar variable "@Name"."

Another example:

-- Will Give Error, as ; is not permitted after GO  
SELECT @@VERSION;  
GO;  
-- Error Incorrect syntax near 'GO'.

The above Query will give an error as we cannot use a semicolon(;) as a statement terminator after GO.

Execute T-SQL multiple times using GO Statement

Another useful function of GO Statement is that it can be used to execute the same SQL commands multiple times.

As GO statement also has an integer optional parameter, this parameter value signals Sql Server to execute the batch of T-Sql Statement prior to the GO statement being executed for the specified number of times.

Example:

SELECT DB_NAME();  
SELECT USER_NAME();  
GO 2  -- Execute above commands twice

Output:

GO-SQl-Server-example

One of the important things to note here is, GO cannot be used inside T-SQL Query, or you can say within Stored Procedure/functions/views etc

So this is not a valid query

CREATE PROCEDURE GOStatementDemo
AS
BEGIN
    SELECT 'Vikram'
    GO  -- Error incorrect syntax near GO
    SELECT 'Vikas'
END

That's it, hope this articles clear details related to GO statement.

You may also like to read:

Extract String after a character or before the character in SQL Server

Convert Int To String (varchar) in SQL Server

If-Else in SQL Server with Example

Find Duplicate Rows in SQL Server

Merge Sort in C#