资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

mysql基础五游标

一、游标的定义:

成都创新互联公司是工信部颁发资质IDC服务器商,为用户提供优质的成都IDC机房托管服务

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;


文章名称:mysql基础五游标
URL链接:http://www.cdkjz.cn/article/pejjij.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220