儲存過程中的游標cursor

2022-08-11 11:36:14 字數 811 閱讀 4625

儲存過程裡的游標,其實就是結果集,然後想操作結果集中的資料,一行行讀取游標即可

首先要宣告乙個游標

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 某變數

儲存過程中帶游標

create procedure updatetimeproperty surroundingrock nvarchar 200 ifclassname nvarchar 200 prop startstation nvarchar 200 prop endstatoin nvarchar 200 ...

儲存過程中的游標使用

利用儲存過程來消除資料庫中冗餘的資料 create procedure sp mytest as declare pro varchar 50 declare mm int declare wu cursor for select distinct product from mytest open ...

儲存過程中游標的使用

例如 乙個公司,按照如下規則計算加薪金額 1.公司中除了總裁 president 外,所有人都會至少增加p min的薪水 2.任何獎金 bonus 高於 600的員工都會另增加4 3.員工的佣金 commission 越高,增加越少。佣金 commission 少於 2000的另增加3 佣金 com...