BEGIN...END

Transact-SQL Reference

Transact-SQL Reference

BEGIN...END

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.

Syntax

BEGIN
    {
        sql_statement
        
| statement_block
    }
END

Arguments

{ sql_statement | statement_block }

Is any valid Transact-SQL statement or statement grouping as defined with a statement block.

Remarks

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 (statement block). For more information, see Batches and the individual statements used.

Examples

In this example, BEGIN and END define a series of Transact-SQL statements that execute together. If the BEGIN...END block were not included, the IF condition would cause only the ROLLBACK TRANSACTION to execute, and the print message would not be returned.

USE pubs
GO
CREATE TRIGGER deltitle
ON titles
FOR delete
AS
IF    (SELECT COUNT(*) FROM deleted, sales
      WHERE sales.title_id = deleted.title_id) > 0
   BEGIN
      ROLLBACK TRANSACTION
      PRINT 'You can't delete a title with sales.'
END

See Also

ALTER TRIGGER

Control-of-Flow Language

CREATE TRIGGER

END (BEGIN...END)