DELIMITER $$DROPPROCEDUREIFEXISTS test_mysql_while_loop$$CREATEPROCEDURE test_mysql_while_loop()BEGINDECLARE x INT;DECLARE str VARCHAR(255);SET x =1;SET str ='';WHILE x <=5 DOSET str =CONCAT(str,x,',');SET x = x +1; ENDWHILE;SELECT str;END$$DELIMITER ;
DELIMITER $$DROPPROCEDUREIFEXISTS mysql_test_repeat_loop$$CREATEPROCEDURE mysql_test_repeat_loop()BEGINDECLARE x INT;DECLARE str VARCHAR(255);SET x =1;SET str ='';REPEATSET str =CONCAT(str,x,',');SET x = x +1; UNTIL x >5ENDREPEAT;SELECT str;END$$DELIMITER ;
CREATEPROCEDURE test_mysql_loop()BEGINDECLARE x INT;DECLARE str VARCHAR(255);SET x =1;SET str =''; loop_label: LOOPIF x >10THEN LEAVE loop_label;ENDIF;SET x = x +1;IF (x mod 2) THEN ITERATE loop_label;ELSESET str =CONCAT(str,x,',');ENDIF;ENDLOOP; SELECT str;END;