Control-of-Flow

Accessing and Changing Relational Data

Accessing and Changing Relational Data

Control-of-Flow

Transact-SQL provides special words called control-of-flow language that control the flow of execution of Transact-SQL statements, statement blocks, and stored procedures. These words can be used in ad hoc Transact-SQL statements, in batches, and in stored procedures.

Without control-of-flow language, separate Transact-SQL statements are performed sequentially, as they occur. Control-of-flow language permits statements to be connected, related to each other, and made interdependent using programming-like constructs.

These control-of-flow words are useful when you need to direct Transact-SQL to take some kind of action. For example, use a BEGIN...END pair of statements when including more than one Transact-SQL statement in a logical block. Use an IF...ELSE pair of statements when a certain statement or block of statements needs to be executed IF some condition is met, and another statement or block of statements should be executed if that condition is not met (the ELSE condition).

The control-of-flow statements cannot span multiple batches or stored procedures.

These are the control-of-flow keywords.

BEGIN...END WAITFOR
GOTO WHILE
IF...ELSE BREAK
RETURN CONTINUE

See Also

Using CASE

CONTINUE

Using GOTO

DECLARE @local_variable

Using RETURN

ELSE (IF...ELSE)

Using WAITFOR

END (BEGIN...END)

Control-of-Flow Language

EXECUTE

-- (Comment)

IF...ELSE

/*...*/ (Comment)

PRINT

BEGIN...END

RAISERROR

BREAK

WHILE