存儲過程游標的使用(6)


需求:編寫存儲過程,使用游標,把uid為偶數的記錄逐一更新用戶名。

Delimiter $$
create procedure testcursor()
Begin
Declare stopflag int default  0;
Declare my_uname varchar(20);
Declare uname_cur cursor for select uname from users where uid%2=0 ; 
#1.游標是保存查詢結果的臨時內存區域,
#2.游標變量uname_cur保存了查詢的臨時結果,實際上就是查詢結果集
declare continue handler for not found set stopflag=1;
#3.當游標變量中保存的結果都查詢一遍(遍歷),到達結尾,把變量stopflag設置為1
#用於循環中判斷是否結束

Open uname_cur; #打開游標
Fetch uname_cur into my_uname; #游標向前走一步,取出一條記錄放到變量my_uname中。
  
  while( stopflag=0 ) do  #如果游標還沒有到結尾,就繼續
  begin
     update testa set uname=concat(my_uname,’_cur’) where uname=my_uname;
     Fetch uname_cur into my_uname;
  end ;
End while;
Close uname_cur;
End;
$$
Delimiter ;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM