MySQL database simple stored procedure cursor usage

Source: Internet
Author: User

BEGIN
#Routine body goes here ...
DECLARE No_more_record INT DEFAULT 0;
DECLARE test_id INT (20);
DECLARE test_username VARCHAR (20);
DECLARE test_sex INT (2);
DECLARE test_address VARCHAR (20);

DECLARE Cur_record CURSOR for SELECT ID, username,sex,address from ' user '; /* First define the cursor here */

DECLARE CONTINUE HANDLER for not FOUND SET No_more_record = 1; /* This is a conditional processing, for the condition of not found, when no record is assigned to 1*/


OPEN Cur_record; /* Next use open cursor */
FETCH Cur_record into test_id, test_username,test_sex,test_address; /* Writes the first row of data to the variable, and the cursor points to the first row of the record */


While No_more_record! = 1 Do
INSERT into TempData (id,username,birthday,sex,address)
VALUES (test_id, Test_username,now (), test_sex,test_address);
FETCH Cur_record into test_id,test_username,test_sex,test_address;

END while;
CLOSE Cur_record; /* Remember to release the resource with close when you're done with it */
END

MySQL database simple stored procedure cursor usage

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.