Mysql loop statements

Source: Internet
Author: User

Mysql LOOP statement ********* * **************** Syntax: java code [begin_label:] LOOP statement_list end loop [end_label] instance: Java code DELIMITER $ create procedure pro_loop (p1 INT) BEGIN label1: loop set p1 = p1 + 1; IF p1 <10 then select p1 AS result; ITERATE label1; end if; SELECT 'end if' AS result; LEAVE label1; end loop label1; SET @ x = p1; SELECT @ x as result; END $ call: call pro_loop (8); output: 9, end if, 10; Description: ITERATE label1: Continue loop label1 LEAVE label1: exit label1 loop Note: The printed Code cannot be output after being put into ITERATE and LEAVE; * **********************while ************* * ************ Syntax: java code [begin_label:] WHILE search_condition DO statement_list end while [end_label] instance: Java code DELIMITER $ drop procedure if exists pro_while $ create procedure pro_while (I INT) begin declare j int default 1; WHILE j <I DO SELECT j AS result; SET j = j + 1; END WHILE; END $ call: call pro_while (3); output: 1, 2 ************************** REPEAT ************ * ************* Syntax: java code [begin_label:] REPEAT statement_list UNTIL search_condition end repeat [end_label] instance: Java code DELIMITER $ drop procedure if exists pro_repeat $ create procedure pro_repeat (p1 INT) begin set @ X = 0; repeat select @ x as result; SET @ X = @ X + 1; UNTIL @ X> p1 end repeat; END $ call: call pro_repeat (2); output: 0, 1, 2 Note: first loop, then judge until @ X> p1 ends the loop

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.