SQL Keywords

SQL WHILE

What is SQL WHILE?

Executes a block of statements repeatedly while a Boolean expression remains TRUE.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL WHILE:

SQL WHILE Full Explanation

SQL WHILE is an imperative control-flow construct that lets you create iterative loops in procedural extensions of SQL. On each iteration the Boolean condition is evaluated; if it returns TRUE the loop body runs, then control returns to the test. When the condition becomes FALSE the loop exits and execution continues with the statement that follows the WHILE block. WHILE is not part of the ANSI-SQL standard but is available in several dialects such as T-SQL (SQL Server and Sybase) and within stored-program languages in MySQL, PostgreSQL, and Oracle. Companion keywords BREAK and CONTINUE (or LEAVE/ITERATE) allow early exit or skipping to the next iteration. Because the same query can be executed set-based, overusing WHILE can hurt performance. Use it only when row-by-row logic, complex procedural branching, or retry logic is unavoidable.

SQL WHILE Syntax

-- T-SQL general form
WHILE Boolean_expression
BEGIN
    -- statements
END;

-- MySQL stored procedure form
WHILE Boolean_expression DO
    -- statements
END WHILE;

SQL WHILE Parameters

  • condition (Boolean expression) - Evaluated before each iteration.
  • statement_block (statement or BEGIN...END block) - Code that runs while the condition is TRUE.

Example Queries Using SQL WHILE

-- Example 1: Basic counter loop in SQL Server
DECLARE @counter INT = 1;
WHILE @counter <= 5
BEGIN
    PRINT CONCAT('Counter = ', @counter);
    SET @counter += 1;
END;

-- Example 2: Retry logic with BREAK
DECLARE @attempts INT = 1;
WHILE @attempts <= 3
BEGIN
    BEGIN TRY
        EXEC usp_ProcessBatch;
        BREAK; -- exit on success
    END TRY
    BEGIN CATCH
        PRINT 'Retrying...';
        SET @attempts += 1;
    END CATCH
END;

Expected Output Using SQL WHILE

  • The first example prints Counter = 1 through Counter = 5 in the Messages tab
  • The second example keeps calling usp_ProcessBatch until it succeeds or three attempts have been made

Use Cases with SQL WHILE

  • Implementing retry logic when calling stored procedures or external resources
  • Performing row-by-row operations that cannot be expressed set-based
  • Building dynamic batches where the iteration count is not known in advance
  • Simulating cursors in lightweight scenarios

Common Mistakes with SQL WHILE

  • Forgetting to update the loop variable, causing infinite loops
  • Using WHILE for tasks that could be done with a single set-based statement, leading to poor performance
  • Assuming WHILE is available in every SQL dialect; it is not supported in SQLite and requires procedural blocks in MySQL/PostgreSQL
  • Misplacing BREAK or CONTINUE so logic flow is unintentionally altered

Related Topics

First Introduced In

Sybase SQL Server 4.x (early T-SQL)

Frequently Asked Questions

What does SQL WHILE do?

It evaluates a condition and runs a statement block repeatedly while that condition stays TRUE.

Can I use WHILE in MySQL?

Yes, but only inside stored procedures, functions, or triggers. The syntax uses WHILE ... DO ... END WHILE.

How do I prevent infinite loops?

Always change the loop variable or include logic that eventually makes the condition FALSE.

What alternatives exist to WHILE loops?

Set-based UPDATE or INSERT statements, window functions, and cursors can often replace explicit loops.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!