Home >Database >Mysql Tutorial >MySQL存储过程中游标循环的跳出和继续操作示例_MySQL

MySQL存储过程中游标循环的跳出和继续操作示例_MySQL

WBOY
WBOYOriginal
2016-06-01 13:06:521122browse

最近遇到这样的问题,在MySQL的存储过程中,游标操作时,需要执行一个conitnue的操作.众所周知,MySQL中的游标循环操作常用的有三种,LOOP,REPEAT,WHILE.三种循环,方式大同小异.以前从没用过,所以记下来,方便以后查阅.

1.REPEAT
复制代码 代码如下:
REPEAT
    Statements;
  UNTIL expression
END REPEAT
demo
DECLARE num INT;
DECLARE my_string  VARCHAR(255);
REPEAT
SET  my_string =CONCAT(my_string,num,',');
SET  num = num +1;
  UNTIL num END REPEAT;

2.WHILE 

复制代码 代码如下:
WHILE expression DO
    Statements;
END WHILE
demo
DECLARE num INT;
DECLARE my_string  VARCHAR(255);
SET num =1;
SET str ='';
  WHILE num  10DO
SET  my_string =CONCAT(my_string,num,',');
SET  num = num +1;
END WHILE;

3.LOOP(这里面有非常重要的ITERATE,LEAVE)

复制代码 代码如下:
DECLARE num  INT;
DECLARE str  VARCHAR(255);
SET num =1;
SET my_string ='';
  loop_label:  LOOP
IF  num       LEAVE  loop_label;
ENDIF;
SET  num = num +1;
IF(num mod3)THEN
      ITERATE  loop_label;
ELSE
SET  my_string =CONCAT(my_string,num,',');
ENDIF;
END LOOP;

PS:可以这样理解ITERATE就是我们程序中常用的contiune,而ITERATE就是break.当然在MySQL存储过程,需要循环结构有个名称,其他都是一样的.

官方的文档-->http://dev.mysql.com/doc/refman/5.0/en/loop.html

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn