Explain Codes LogoExplain Codes Logo

What is the difference between ";" and "GO" in T-SQL?

sql
sql-server
t-sql
batch-commands
Nikita BarsukovbyNikita Barsukov·Jan 8, 2025
TLDR

A semicolon ; is a statement separator marking the end of individual T-SQL statements. Useful when crafting complex statements or combining multiple queries in a single line.

Conversely, GO instructs SQL Server Management Studio (SSMS) or SQLCMD utility to ship the current batch of T-SQL statements for execution. It's not recognized by SQL Server and can't be parsed as valid T-SQL—yet it proves critical in managing "batches" of statements.

Example with ;:

-- Alice won't be attending the party, let's invite Bob instead INSERT INTO Users (Name) VALUES ('Alice'); DELETE FROM Users WHERE Name = 'Bob';

Example with GO:

-- What are we doing? We're just inviting Alice INSERT INTO Users (Name) VALUES ('Alice') GO -- And... Bob's not coming. DELETE FROM Users WHERE Name = 'Bob' GO

Taking a deeper dive into ";" and "GO"

Unpacking the ";" in T-SQL

The semicolon ; is like a small whistle, bringing a single T-SQL statement to a halt. It's used predominantly for clarity, especially when statements are complex or multiple queries reside on one line. For instance:

-- Busy day? Use CTE to keep track of your meetings! WITH CTE AS ( SELECT * FROM Employees WHERE EmployeeId = 1 ) SELECT * FROM CTE;

Indeed, it's not yet mandatory to use semicolons in SQL Server, but it's a good habit to cultivate. They can prevent certain types of errors from cropping up in the future.

The big role of "GO" in batching commands

The GO command is like a traffic light, managing the flow of your SQL script. It's not part of T-SQL—rather, it's a signal for client tools like SSMS or sqlcmd. "GO" informs them to submit the batch of T-SQL statements accumulated so far to SQL Server.

-- Create a table, one column at a time. Rome wasn't built in a day 🏛️😉 CREATE TABLE TempTable (ID INT); GO -- TempTable is now created and ready for subsequent commands. SET @Variable = 'Test'; GO -- This is an error, since variables are valid only within the batch that defines them.

We can also make coding fun by asking "GO" to repeat the batch of commands a specific number of times:

-- Weekend vibes at the office 😴 PRINT 'Friday, punch out time!' GO 5 -- This will print the statement five times.

If you've decided to create or alter schema objects like tables and stored procedures, remember the GO command comes to the rescue. It splits the SQL script into manageable units and ensures all preceding commands are properly executed and committed:

CREATE TABLE MyTable (ID INT); GO ALTER TABLE MyTable ADD Name NVARCHAR(100); GO

Without GO between CREATE and ALTER, SQL Server might throw an error because it interprets the entire script as a single batch.

Pitstop: How "GO" affects transactions

Contrary to some beliefs, GO does not commit open transactions. It's merely a signal to SSMS or SQLCMD to send the completed batch to SQL Server.

-- No getting off halfway on this transaction train 🚂 BEGIN TRANSACTION INSERT INTO Users (Name) VALUES ('Alice') GO COMMIT TRANSACTION

In this example, GO ends up causing an error because you aren't allowed to split a transaction between batches.

Touring through multiple result sets

"GO" allows your SQL script to produce multiple result sets, and each is handled as a separate unit by the client application:

-- This would generate two separate datasets—kind of like a buy one get one free offer! SELECT * FROM Users WHERE Name = 'Alice' GO SELECT * FROM Products WHERE Category = 'Electronics'

Traveling with clients: ADO.NET and "GO"

Remember, if your journey includes client-side tools like ADO.NET, they don't speak the language of "GO". It's a term understood only by SQL Server-centric tools like SSMS and sqlcmd.