当我使用游标通过重复获取数据时,我的代码之间有什么区别?
What is the differece between my codes when I use cursor to fetch my data with repeat and do while?
我使用游标来获取我的 datas.The 我的数据数量是 two.But 我发现当我使用 repeat
来获取我的数据时,它总是获取 3 次。当我使用 do while
时,fetch 2 times.What 是这两种方式的不同之处吗?如何修复第一种方式?
--- 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 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;
只需要在execute语句前加一个justment即可。
open v_result_cur;
repeat
fetch v_result_cur into v_id;
IF NOT v_done THEN
select v_id;
END IF;
until v_done end repeat;
close v_result_cur;
我使用游标来获取我的 datas.The 我的数据数量是 two.But 我发现当我使用 repeat
来获取我的数据时,它总是获取 3 次。当我使用 do while
时,fetch 2 times.What 是这两种方式的不同之处吗?如何修复第一种方式?
--- 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 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;
只需要在execute语句前加一个justment即可。
open v_result_cur;
repeat
fetch v_result_cur into v_id;
IF NOT v_done THEN
select v_id;
END IF;
until v_done end repeat;
close v_result_cur;