存储过程中的游标cursor

存储过程里的游标,其实就是结果集,然后想操作结果集中的数据,一行行读取游标即可

首先要声明一个游标

delimiter $$
CREATE procedure changeName()
begin
declare stopflag int default 0;
declare myname varchar(20) default '';
declare my_cursor cursor for select sname from student where sid%2=0;
declare continue handler for not found set stopflag=1;
open my_cursor;
while(stopflag=0) do
begin
fetch my_cursor into myname;
update student set sname = concat(sname,'aab') where sname = myname;
end;
end while;
close my_cursor;
end;
$$

以上是对游标的一个使用,

注意点:

游标的声明需要在所有其他变量之后,游标常常要配合一个状态值来实现功能,需要声明一个休止标识

declare continue handler for not found set 休止标识=1;

游标使用要先open,最后需要close

游标值的获取用fetch my_cursor into 某变量

猜你喜欢

转载自www.cnblogs.com/liuyongbo/p/11004043.html
今日推荐