I use cursor to fetch my datas.The number of my data is two.But I find that when I use repeat
to fetch my data,it always fetch 3 times. And When I use do while
,it is right which fetch 2 times.What is the different of thest two ways?How to fixed the first way?
--- The first way:wrong,have two datas but show 3 times
declare v_done tinyint default 0;
declare v_count int default 0;
declare v_id varchar(32);
declare v_result_cur cursor for select distinct(id) from book;
declare continue handler for not found set v_done = 1;
BEGIN
open v_result_cur;
repeat
fetch v_result_cur into v_id;
select v_id;
until v_done end repeat;
close v_result_cur;
END;
The first way is wrong and the second way is right.
--- The second way:right,have two datas but show 2 times
BEGIN
declare v_done tinyint default 0;
declare v_count int default 0;
declare v_id varchar(32);
declare v_result_cur cursor for select distinct(id) from book;
BEGIN
open v_result_cur;
dept_loop:WHILE(v_done=0) DO
fetch v_result_cur into v_id;
select v_id;
IF v_done=1 THEN
LEAVE dept_loop;
END IF;
END WHILE dept_loop;
close v_result_cur;
END;
Just add a justment before the execute statement.