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