The REPEAT statement is used to repeatedly execute a statement until the condition is true.
Syntax
[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]
Parameters
Parameter |
Description |
|---|---|
| begin_label | Optional. The label name at the beginning of the loop. |
| statement_list | The list of statements to be repeated. Each statement ends with a semicolon (;). |
| search_condition | The condition expression, evaluated at the end of each loop iteration. If the result is true, the loop exits; if false, the loop continues to the next iteration. |
| end_label | Optional. The label name at the end of the loop, which must match begin_label. |
The statement_list in the REPEAT statement is repeatedly executed until the search_condition expression is true. Therefore, the REPEAT loop will execute at least once. The statement_list consists of one or more statements, each ending with a semicolon (;). You can use label to mark the REPEAT statement.
Examples
Execute statements using the REPEAT loop until the condition is met.
obclient> DELIMITER //
obclient> CREATE PROCEDURE rep_proc(p1 INT)
BEGIN
SET @x = 0;
REPEAT
SET @x = @x + 1;
UNTIL @x > p1 END REPEAT;
END //
obclient> DELIMITER ;
obclient> CALL rep_proc(100);
obclient> SELECT @x;
The query result is as follows:
+------+
| @x |
+------+
| 101 |
+------+
1 row in set
