1. while loop DELIMITER $ DROPPROCEDUREIFEXISTS 'sp _ test_while '$ CREATEPROCEDURE 'sp _ test_while' (INp_numberINT, # The number of times INp_startidINT # the actual value of the loop) BEGINDECLARE...
Mysql while, loop, repeat loop, conforming to the condition to jump out of the loop _ MySQL
1. while loop
DELIMITER $ drop procedure if exists 'sp _ test_while '$ create procedure 'sp _ test_while' (IN p_number INT, # The number of times to be cyclically IN p_startid INT # the actual value of the loop) begin declare v_val int default 0; SET v_val = p_startid; outer_label: BEGIN # SET a flag WHILE v_val <= p_number do set v_val = v_val + 1; IF (v_val = 100) then leave outer_label; # IF the conditions are met, terminate the loop and jump to the end outer_label label to mark end if; end while; SELECT 'SQL in outer_label and out of while '; # because this SQL statement is in the outer_label code block, this SQL statement will not be executed after the level; # as long as it is in any position in the outer_label code block Leave outer_label, then the Leave code will not execute END outer_label; select concat ('test', v_val) AS tname; END $ DELIMITER; CALL sp_test_while );
2. loop
DELIMITER $ drop procedure if exists 'sp _ testloop '$ create procedure 'sp _ testloop' (IN p_number INT, # The number of times to be cyclically IN p_startid INT # the actual value of the loop) begin declare v_val int default 0; SET v_val = p_startid; loop_label: LOOP # SET v_val = v_val + 1; IF (v_val> p_number) then leave loop_label; # end if; end loop; select concat ('testloop _ ', v_val) AS tname; END $ delimiter; CALL sp_testloop );
3. repeat loop
DELIMITER $ drop procedure if exists 'sp _ test_repeat '$ create procedure 'sp _ test_repeat' (IN p_number INT, # Number of times IN p_startid INT # actual value of the loop) begin declare v_val int default 0; SET v_val = p_startid; REPEAT # SET v_val = v_val + 1 when the repeat loop starts; until v_val> p_number # when the loop ends, note that '; 'Semicolon; otherwise, the END repeat; # loop end select concat ('test', v_val) AS tname; END $ DELIMITER; CALL sp_test_repeat );
The above is the mysql while, loop, repeat loop, conforming to the condition to jump out of the loop _ MySQL content. For more information, please follow the PHP Chinese network (www.php1.cn )!