mysql基础五 游标
一、游标的定义:
create procedure p12()
begin
declare row_name varchar(20);
declare row_num int;
declare myCursor cursor for select name,num from goods;//定义游标myCursor
open myCursor;//打开游标myCursor
fetch myCursor into row_name,row_num;//使用游标myCursor获取第一行
select row_name, row_num;
fetch myCursor into row_name,row_num;//使用游标myCursor获取第二行;每fetch一次游标就自动往下游一次.
select row_name, row_num;
close myCursor;//关闭游标myCursor
end;
二、游标+repeat循环-->实现遍历行:
create procedure p13()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare row_count int;
declare i int default 0;
declare myCursor cursor for select gid,name,num from goods;
select count(1) into row_count from goods;
open myCursor;
repeat
fetch myCursor into row_gid,row_name,row_num;
select row_gid,row_name,row_num;
set i=i+1;
until i>row_count end repeat;
close myCursor;
end;
三、游标+continue handler实现遍历行:
continue handler 当fetch触发此handler后,后面的语句继续执行。
所以会多执行一次select row_gid,row_name,row_num;
此handler常用。
create procedure p15()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare you int default 1;
declare myCursor cursor for select gid,name,num from goods;
declare continue handler for NOT FOUND set you=0;
open myCursor;
repeat
fetch myCursor into row_gid,row_name,row_num;
select row_gid,row_name,row_num;
until you=0 end repeat;
close myCursor;
end;
四、游标+exit handler实现遍历行:
exit handler 当fetch触发此handler后,触发后后面的语句不再执行。
所以select row_gid,row_name,row_num;不会再被执行。
此handler不常用。
create procedure p16()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare you int default 1;
declare myCursor cursor for select gid,name,num from goods;
declare exit handler for NOT FOUND set you=0;
open myCursor;
repeat
fetch myCursor into row_gid,row_name,row_num;
select row_gid,row_name,row_num;
until you=0 end repeat;
close myCursor;
end;
五、游标+while实现遍历行:
create procedure p15()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare over int default 1;
declare myCursor cursor for select gid,name,num from goods;
declare continue handler for NOT FOUND set over =0;
open myCursor;
fetch myCursor into row_gid,row_name,row_num;
while over do
select row_gid,row_name,row_num;
fetch myCursor into row_gid,row_name,row_num;
end while;
close myCursor;
end;
六、游标+loop实现遍历行:
-- loop 与 leave,iterate 实现循环
-- loop 标志位无条件循环;leave 类似于Java break 语句,跳出循环,即跳出 begin end;
iterate 类似于java continue ,结束本次循环,继续下一次循环。
--loop的优点在于可以根据条件结束本次循环或者根据条件跳出循环。
create procedure p17()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare over int default 0;
declare myCursor cursor for select gid,name,num from goods;
declare continue handler for NOT FOUND set over=1;
open myCursor;
cursor_loop:loop
fetch myCursor into row_gid,row_name,row_num;
if over then
leave cursor_loop;
end if;
select row_gid,row_name,row_num;
end loop cursor_loop;
close myCursor;
end;
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341