Mysql while, loop, repeat loop, conforming to the condition to jump out of the loop, mysqlrepeat

Source: Internet
Author: User

Mysql while, loop, repeat loop, conforming to the condition to jump out of the loop, mysqlrepeat
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 );

Related Article

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.