This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Download Microsoft Edge
More info about Internet Explorer and Microsoft Edge
Azure Synapse Analytics
Analytics Platform System (PDW)
Encloses a series of Transact-SQL statements so that a group of Transact-SQL statements can be executed. BEGIN and END are control-of-flow language keywords.
Transact-SQL syntax conventions
Syntax
BEGIN
{ sql_statement | statement_block }
To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
Arguments
{ sql_statement | statement_block }
Is any valid Transact-SQL statement or statement grouping as defined by using a statement block.
BEGIN...END blocks can be nested.
Although all Transact-SQL statements are valid within a BEGIN...END block, certain Transact-SQL statements should not be grouped together within the same batch, or statement block.
Examples
In the following example, BEGIN
and END
define a series of Transact-SQL statements that execute together. If the BEGIN...END
block were not included, both ROLLBACK TRANSACTION
statements would execute and both PRINT
messages would be returned.
USE AdventureWorks2012
BEGIN TRANSACTION
IF @@TRANCOUNT = 0
BEGIN
SELECT FirstName, MiddleName
FROM Person.Person WHERE LastName = 'Adams';
ROLLBACK TRANSACTION;
PRINT N'Rolling back the transaction two times would cause an error.';
ROLLBACK TRANSACTION;
PRINT N'Rolled back the transaction.';
Rolled back the transaction.
Examples: Azure Synapse Analytics and Analytics Platform System (PDW)
In the following example, BEGIN
and END
define a series of SQL statements that run together. If the BEGIN...END
block are not included, the following example will be in a continuous loop.
-- Uses AdventureWorks
DECLARE @Iteration Integer = 0;
WHILE @Iteration <10
BEGIN
SELECT FirstName, MiddleName
FROM dbo.DimCustomer WHERE LastName = 'Adams';
SET @Iteration += 1 ;
See Also
ALTER TRIGGER (Transact-SQL)
Control-of-Flow Language (Transact-SQL)
CREATE TRIGGER (Transact-SQL)
END (BEGIN...END) (Transact-SQL)