存储过程中的游标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 某变量

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM