begin...end

Description

Encloses a series of SQL statements so that control-of-flow language, such as if...else, can affect the performance of the whole group.

Syntax

begin 
	statement block
end

Parameters

statement block

is a series of statements enclosed by begin and end.

Examples

Example 1

Without begin and end, the if condition would cause execution of only one SQL statement:

if (select avg (price) from titles) < $15 
begin 
    update titles 
    set price = price * $2
    select title, price 
    from titles 
    where price > $28 
end

Example 2

Without begin and end, the print statement would not execute:

create trigger deltitle 
on titles 
for delete 
as 
if (select count (*) from deleted, salesdetail 
 where salesdetail.title_id = deleted.title_id) > 0 
    begin 
        rollback transaction 
        print "You can’t delete a title with sales." 
    end 
else 
    print "Deletion successful--no sales for this
        title."

Usage

Standards

ANSI SQL – Compliance level: Transact-SQL extension.

Permissions

No permission is required to use begin...end.

See also

Commands if...else