Mysqlwhile, loop, repeat loop, conforming to the condition to jump out of the loop _ MySQL

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.