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