最近遇到这样的问题,在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 <5
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 < span>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 <10THEN
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存储过程,需要循环结构有个名称,其他都是一样的.