13.6.5.5 LOOP Statement语句

[begin_label:] LOOP
statement_list
END LOOP [end_label]

LOOP implements a simple loop construct, enabling repeated execution of the statement list, which consists of one or more statements, each terminated by a semicolon (;) statement delimiter. LOOP实现了一个简单的循环构造,允许重复执行语句列表,语句列表由一个或多个语句组成,每个语句以分号(;)结尾语句分隔符。The statements within the loop are repeated until the loop is terminated. 循环中的语句将重复,直到循环终止。Usually, this is accomplished with a LEAVE statement. 通常,这是通过LEAVE声明来完成的。Within a stored function, RETURN can also be used, which exits the function entirely.在存储函数中,还可以使用RETURN,它将完全退出函数。

Neglecting to include a loop-termination statement results in an infinite loop.忽略包含循环终止语句会导致无限循环。

A LOOP statement can be labeled. 可以标记LOOP语句。For the rules regarding label use, see Section 13.6.2, “Statement Labels”.有关标签使用的规则,请参阅第13.6.2节,“语句标签”

Example:例子:

CREATE PROCEDURE doiterate(p1 INT)
BEGIN
  label1: LOOP
    SET p1 = p1 + 1;
    IF p1 < 10 THEN
      ITERATE label1;
    END IF;
    LEAVE label1;
  END LOOP label1;
  SET @x = p1;
END;