Export (0) Print
Expand All
Expand Minimize
8 out of 9 rated this helpful - Rate this topic

BEGIN...END (Transact-SQL)

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.

Topic link iconTransact-SQL Syntax Conventions


BEGIN
     { 
    sql_statement | statement_block 
     } 
END
{ 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. For more information, see Batches and the individual statements used.

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 AdventureWorks;
GO
BEGIN TRANSACTION;
GO
IF @@TRANCOUNT = 0
BEGIN
SELECT * from Person.Contact WHERE LastName = 'ADAMS';
ROLLBACK TRANSACTION
PRINT N'Rolling back the transaction two times would cause an error.'
END
ROLLBACK TRANSACTION
PRINT N'Rolled back the transaction.'
GO
/*
Rolled back the tranaction.
*/
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.