LOOP

2024-06-28 05:30:31  Updated

You can use the LOOP statement to build a simple loop statement.

[begin_label:] LOOP
    statement_list
END LOOP [end_label]

The LOOP statement supports the repeated execution of statement_list. statement_list consists of one or more SQL statements and each SQL statement ends with a semicolon (;). A loop statement will be executed repeatedly until you use the LEAVE statement to end the loop. You can also use the RETURN statement to exit a stored function. The absence of a loop end statement will cause an infinite loop. You can use label to label a LOOP statement.

Here is an example:

obclient> DELIMITER //

obclient> CREATE PROCEDURE iter_proc(p1 INT)
  BEGIN
    loop_label1: LOOP
      SET p1 = p1 + 10;
      IF p1 < 100 THEN
        ITERATE loop_label1;
      END IF;
      LEAVE loop_label1;
    END LOOP loop_label1;
    SET @x = p1;
  END //
Query OK, 0 rows affected

Contact Us