Loop operations in MySQL stored procedures

Source: Internet
Author: User
Execute loop operations with the cursor Drop   Procedure   If   Exists Sp_opr;
Create   Procedure Sp_opr ()
Begin

/**/ /*Define variable 1*/
Declare _ Err_code Char ( 8 ) Default   '' ;
/**/ /*Define variable 2*/
Declare _ CNT Int   Default   0 ;

/**/ /*Cursor*/
Declare _ Done Int   Default   0 ;
Declare _ Cur_error Cursor   For
Select Col1, col2 From   Table   Where Rec_dd = Adddate (curdate (), - 1 );
Declare   Continue Handler For Sqlstate ' 02000 '   Set _ Done =   1 ;

/**/ /*Open cursor*/
Open _ Cur_error;
/**/ /*Loop execution*/
Repeat
Fetch _ Cur_error Into _ Err_code, _ CNT; -- Read the next row

If   Not _ Done Then
-- The code here is executed cyclically (the value read above can be used here)
End   If ;

Until _ done End Repeat; -- When _ DONE = 1, the system exits

/**/ /*Close the cursor*/
Close _ Cur_error;

End ;

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.