The MySQL stored procedure cursor operation jumps and continues

Source: Internet
Author: User

Recently encountered such a problem, in the MySQL stored procedure, the cursor operation, you need to perform a conitnue operation. As we all know, there are three kinds of cursor loops in MySQL, loop,repeat,while three loops, the same way. Never used before, So write it down and make it easier to consult later.

1.REPEAT

The code is as follows Copy Code

REPEAT
statements;
UNTIL expression
End REPEAT
Demo
DECLARE num INT;
DECLARE my_string VARCHAR (255);
REPEAT
SET my_string =concat (my_string,num, ', ');
SET num = num +1;
UNTIL Num <5
End Repeat;2.while


While expression do
statements;
End While
Demo
DECLARE num INT;
DECLARE my_string VARCHAR (255);
SET num = 1;
SET str = ';
While Num < Span>10do
SET my_string =concat (my_string,num, ', ');
SET num = num +1;
End While;3.loop (which has very important iterate,leave)


DECLARE num INT;
DECLARE str VARCHAR (255);
SET num = 1;
SET my_string = ';
Loop_label:loop
IF Num <10then
LEAVE Loop_label;
ENDIF;
SET num = num +1;
IF (num mod3) THEN
Iterate Loop_label;
ELSE
SET my_string =concat (my_string,num, ', ');
ENDIF;
End LOOP;


PS: This can be understood iterate is our program commonly used in Contiune, and iterate is break. Of course, in the MySQL stored procedure, you need to have a name for the loop structure, all the others are the same.

About: Cursors and multiple-cursor instances in MySQL stored procedures

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.