WHILE (TSQL Statement)

is a loop statement, which iterates as long as condition is True.

Syntax

WHILE condition
    <statement>

Arguments

condition
is a condition expression of type bool.
<statement>
any valid TSQL statement or BEGIN...END block of statements.

Remarks

The statements BREAK and CONTINUE can be used to control the execution of the loop.

Examples

DECLARE @i INT = 0;

WHILE @i < 1000
  BEGIN
  PRINT @i;
  IF @i = 4
    BREAK;

  SET @i += 1;
  END

PRINT 'End';

The result is:

0
1
2
3
4
End