1. 程式人生 > >儲存過程利用游標迴圈

儲存過程利用游標迴圈

CREATE DEFINER=`root`@`localhost` PROCEDURE `update_mt_begin_time_is_six`()
BEGIN
#	載入完畢第一次時間表mt_begin_time 判斷是否在六環內
DECLARE  cur_id int(11); #	游標定義的id
DECLARE  cur_linkcode varchar(100);#	游標定義的linkcode
DECLARE  search_area_hd int(11); #	轉換的地區值 用於條件查詢
DECLARE  cur_record CURSOR FOR select id,link_id from mt_begin_time; #	定義游標
DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_record; #	異常處理
OPEN  cur_record;#開啟游標
REPEAT
	FETCH  cur_record INTO cur_id,cur_linkcode;
	SELECT CONVERT(`area_hd`,SIGNED) INTO search_area_hd  FROM jing_vehicle_result_mapping WHERE link_id=cur_linkcode;
		IF(search_area_hd>0) THEN UPDATE mt_begin_time SET is_six = 1 WHERE id=cur_id;
			ELSE UPDATE mt_stay_bj SET is_six = 0 WHERE id=cur_id;
		END IF;
UNTIL	cur_id=null END REPEAT; #	迴圈結束
CLOSE cur_record; #	關閉遊標
END