mysql 遊標例項
阿新 • • 發佈:2018-12-10
遊標(Cursor)是處理資料的一種方法,為了檢視或者處理結果集中的資料,遊標提供了在結果集中一次一行或者多行前進或向後瀏覽資料的能力。可以把遊標當作一個指標,它可以指定結果中的任何位置,然後允許使用者對指定位置的資料進行處理。
--declare
--open
--fetch
--close
create procedure p12() begin declare row_gid int; declare row_num int; declare row_name varchar(20); declare getgoods cursor for select gid,num,name from goods; open getgoods; fetch getgoods into row_gid,row_num,row_name; select row_num,row_name; close getgoods; end$ create procedure p14() begin declare row_gid int; declare row_num int; declare row_name varchar(20); declare cnt int default 0; declare i int default 0; declare getgoods cursor for select gid,num,name from goods; select count(*) into cnt from goods; open getgoods; repeat set i:=i+1; fetch getgoods into row_gid,row_num,row_name; select row_num,row_name; until i>=cnt end repeat; close getgoods; end$ ########### bug 版本 ########## create procedure p15() begin declare row_gid int; declare row_num int; declare row_name varchar(20); declare you int default 1; declare getgoods cursor for select gid,num,name from goods; declare continue handler for NOT FOUND set you:=0; open getgoods; repeat fetch getgoods into row_gid,row_num,row_name; select row_num,row_name; until you=0 end repeat; close getgoods; end$ call p15(); create procedure p16() begin declare row_gid int; declare row_num int; declare row_name varchar(20); declare you int default 1; declare getgoods cursor for select gid,num,name from goods; declare exit handler for NOT FOUND set you:=0; open getgoods; repeat fetch getgoods into row_gid,row_num,row_name; select row_num,row_name; until you=0 end repeat; close getgoods; end$