The REPEAT statement is used to repeatedly execute statements until the condition becomes true, at which point the loop exits.
Syntax
[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]
Parameters
Parameter |
Note |
|---|---|
| begin_label | Optional. The name of the tag at the start of the loop. |
| statement_list | A list of statements to be executed repeatedly. Each statement ends with a semicolon (;). |
| search_condition | The conditional expression is evaluated at the end of each iteration. If the result is True, the loop exits; if it is False, the next iteration continues. |
| end_label | Optional. The tag name at the end of the loop must be consistent with the begin_label The same. |
The statement_list in a REPEAT statement is executed repeatedly until the search_condition expression evaluates to True. Therefore, the REPEAT loop always executes at least once. The statement_list consists of one or more statements, each ending with a semicolon (;). You can use a label to mark the REPEAT statement.
Examples
Use a REPEAT loop to execute statements until a 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