begin declare temp_user_id int default null; declare stop int default 0; #聲明游標 declare temp_cur cursor for select f_user_id from table_test where f_user_id=1; #聲明游標的異常處理 declare continue handler for sqlstate '02000' set stop=1; open temp_cur; fetch temp_cur into temp_user_id; #判斷游標是否到達最后 while stop<>1 do #各種判斷 #讀取下一行的數據 fetch temp_cur into temp_user_id; #循環結束 end while; #關閉游標 close temp_cur; end