SQL Flow Field Guide

WHILE (and CONTINUE)

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

loop while TRUE; BREAK exits, CONTINUE restarts

What changes

Input
boolean condition
Output or change
none

Syntax at a glance

WHILE boolean_expression { sql_statement | statement_block | BREAK | CONTINUE }

Work example

A controlled task needs the behavior documented for WHILE (and CONTINUE) before it is added to a batch.

Read the linked Microsoft syntax and test WHILE (and CONTINUE) with synthetic input in an isolated database. Record its scope, permissions, and error path.

Check your work WHILE (and CONTINUE). The observed WHILE (and CONTINUE) behavior matches its written purpose and the test has no effect on permanent data or credentials.

Practice SQL

Open in SQL Flow
DECLARE @Step int = 1;
WHILE @Step <= 3
BEGIN
    SELECT @Step AS StepNumber;
    SET @Step += 1;
END;

Open Microsoft Learn for full syntax, permissions, and details for your version.