当前位置:Gxlcms > 数据库问题 > mysql基础五 游标

mysql基础五 游标

时间:2021-07-01 10:21:17 帮助过:18人阅读

一、游标的定义:

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;

mysql基础五 游标

标签:游标

人气教程排行