BEGIN...END (T-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.

Syntax

BEGIN
    {
        sql_statement
        
| statement_block
    }
END

Argument
{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 being used.

Examples

In this example, BEGIN and END define a series of Transact-SQL statements that are to be executed 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 CREATE TRIGGER
Control-of-Flow Language END (BEGIN...END)

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.