MySql stored procedure-6. Loop

Source: Internet
Author: User


MySql stored procedure-6. Loop links: MySql stored procedure-1. Basic knowledge of SQL stored procedure http://www.bkjia.com/database/201208/148790.html ; MySql stored procedure-2. Creation of the first MySql Stored Procedure http://www.bkjia.com/database/201208/148791.html ; MySql stored procedure-3. Variables http://www.bkjia.com/database/201208/149069.html ; MySql stored procedure-4. Parameters http://www.bkjia.com/database/201208/149113.html MySql stored procedure-5. logical judgment and condition Control http://www.bkjia.com/database/201208/149282.html There are three types of loops that can be used in the MySql stored procedure: WHILE, REPEAT, LOOP 1, WHILE: [SQL] WHILE expression DO Statements end while below is an example [SQL] DELIMITER $ www.2cto.com DROP PROCEDURE IF EXISTS 'test '. 'whileloopproc' $ create procedure 'test '. 'whileloopproc' () begin declare x INT; DECLARE str VARCHAR (255); SET x = 1; SET str = ''; WHILE x <= 5 do set str = CONCAT (str, x, ','); SET x = x + 1; end while; SELECT str; END $ DELIMITER; in the example above, the character "1, 2, 3, 4, 5," is output. 2. The REPEAT format is as follows: www.2cto.com [SQL] REPEAT Statements; UNTIL expression END REPEAT is like do... while in our programming. This example will not be used. It is similar to the above. 3. LOOP, LEAVE, and ITERATE here the LOOP is used to mark the LOOP; while LEAVE indicates to LEAVE the LOOP, just like break in programming; ITERATE indicates to continue the LOOP, and the type and the continue in programming. [SQL] DELIMITER $ DROP PROCEDURE IF EXISTS 'test '. 'loopproc' $ create procedure 'test '. 'loopproc' () begin declare x INT; DECLARE str VARCHAR (255); SET x = 1; SET str = ''; loop_label: loop if x> 10 then leave loop_label; end if; SET x = x + 1; IF (x mod 2) then iterate loop_label; ELSE www.2cto.com SET str = CONCAT (str, x, ','); end if; end loop; SELECT str; END $ DELIMITER; the above Code outputs an even number within 10, which is separated by commas.. It is noted that LEAVE is dropped when x> 10. If an odd number is encountered, ITERATE is used. If an odd number is used, the modulo of 2 is 1, indicating true.

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.