Advertisement
Intermediate SQL Server, T-SQL Fundamentals & Syntax

What is the difference between a batch and a transaction in T-SQL, and how does the GO keyword relate to batches?

Short answer

A batch is a group of one or more T-SQL statements sent to SQL Server together for parsing and execution as a single unit, separated using the GO keyword, which is actually a client tool command rather than real T-SQL, while a transaction is a separate concept controlling…

Save SQL Server mock interview
Advertisement
AModel answer
A batch is a group of one or more T-SQL statements sent to SQL Server together for parsing and execution as a single unit, separated using the GO keyword, which is actually a client tool command rather than real T-SQL, while a transaction is a separate concept controlling atomicity of data changes, and a single batch can contain zero, one, or several transactions within it.
DECLARE @Message VARCHAR(50) = 'Hello';
PRINT @Message;
GO
-- This starts a new batch, and @Message is no longer in scope here
PRINT @Message; -- this would cause an error
Real-world example
A developer troubleshooting a confusing error learns that a variable declared before a GO statement is not available afterward, since GO starts an entirely new batch with its own separate variable scope.
Likely follow-ups
  • Why does a variable's scope end at a GO statement?
  • Can you use GO inside a stored procedure definition?
Related topics