REPEAT

2024-12-02 03:48:27  Updated

You can use the REPEAT statement to execute a statement repeatedly.

The syntax is as follows:

[begin_label:] REPEAT
    statement_list
UNTIL search_condition
END REPEAT [end_label]

statement_list in the REPEAT statement is executed repeatedly until the result of the search_condition expression is True. Therefore, REPEAT will enter a loop at least once. statement_list consists of one or more SQL statements. Each SQL statement ends with a semicolon (;). You can use label to label a REPEAT statement.

Here is an example:

obclient> DELIMITER //

obclient> CREATE PROCEDURE rep_proc(p1 INT)
       BEGIN
         SET @x = 0;
         REPEAT
           SET @x = @x + 1;
         UNTIL @x > p1 END REPEAT;
       END
       //
Query OK, 0 rows affected

obclient> CALL rep_proc(100)//
Query OK, 0 rows affected

obclient> SELECT @x//
+------+
| @x   |
+------+
| 101  |
+------+
1 row in set

Contact Us