存儲過程中的游標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