The last line of the MySQL cursor repeats

Source: Internet
Author: User

BEGIN
DECLARE p_id INT;
DECLARE p_code INT default 0;
DECLARE P_logo INT default 0;
DECLARE p_name varchar (255) default ';
DECLARE p_address varchar (755) default ';
DECLARE p_phone varchar (755) default ';
DECLARE p_logo2 varchar (755) default ';
DECLARE P_LOGO3 varchar (755) default ';
DECLARE p_paid varchar (755) default ';
DECLARE p_isdelete varchar (755) default ';
Declare done INT DEFAULT 0;
DECLARE cur CURSOR for SELECT ID, code,logo,name,address,phone,logo2,logo3,paid,isdelete from Hospital_copy;
Declare CONTINUE HANDLER for not FOUND SET done=1;
OPEN cur;
REPEAT
FETCH cur into p_id, p_code,p_logo,p_name,p_address,p_phone,p_logo2,p_logo3,p_paid,p_isdelete;
IF done=0 Then
INSERT into ' Hospital ' (
' ID ',
' Code ',
' Name ',
' Country_code ',
' Phone ',
' Address1 ',
' Address2 ',
' Address3 ',
' County ',
' Country ',
' postcode ',
' Logo ',
' Logo2 ',
' Logo3 ',
' Paid ',
' IsDeleted ',
' Add_time ',
' Delete_time ',
' TimeInterval '
)
VALUES (
p_id, P_code,p_name, ' 0001 ', ' 13971056776 ', p_address,p_address, p_address, p_address, p_address, ' 442200 ', P_Logo, P_logo2,p_logo3,p_paid,p_isdelete, ' 2011-1-1 ', ', ', ');
END IF;
UNTIL done=1
END REPEAT;
CLOSE cur;
END

To deepen the red part of the solution, the last repetition is the reason for the fetch cur into ...; , when the last record is first reached, the record is taken, the value is given to the variable, and everything is OK and there is no error. Then UNTIL UNTIL done=1 loop to repeat restart, then fetch one, there is no record, done will be SET done=1, but at the moment no judgment, the program will continue to execute insert INTO ... statement, and since fetch is not taken to a record, the variable is not assigned, so the variable is still the original value. The program exits repeat when it is done to until, and the final record is duplicated.

The last line of the MySQL cursor repeats

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.