# WHILE

## Syntax

```sql
[begin_label:] WHILE search_condition DO
    statement_list
END WHILE [end_label]
```

## Description

The statement list within a `WHILE` statement is repeated as long as the`search_condition` is true. statement\_list consists of one or more statements. If the loop must be executed at least once, [REPEAT ... LOOP](/docs/server/reference/sql-statements/programmatic-compound-statements/repeat-loop.md) can be used instead.

A `WHILE` statement can be [labeled](/docs/server/reference/sql-statements/programmatic-compound-statements/labels.md). `end_label` cannot be given unless `begin_label` also is present. If both are present, they must be the same.

## Examples

```sql
CREATE PROCEDURE dowhile()
BEGIN
  DECLARE v1 INT DEFAULT 5;

  WHILE v1 > 0 DO
    ...
    SET v1 = v1 - 1;
  END WHILE;
END
```

<sub>*This page is licensed: GPLv2, originally from*</sub> [<sub>*fill\_help\_tables.sql*</sub>](https://github.com/MariaDB/server/blob/main/scripts/fill_help_tables.sql)

{% @marketo/form formId="4316" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/while.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
