drop procedure count_points; create procedure count_points() begin declare n_Latitude double; declare n_Longitude double; declare isExist int; declare countExist int default 0; declare n_cellname varchar(255); -- 定義遊標遍歷時,做爲判讀是否遍歷徹底被記錄的標記 declare num int default 0; declare lat_long_list CURSOR FOR select Longitude,Latitude,cellname from table_name; -- 聲明當遊標遍歷徹底被記錄後將標誌變量置成某個值 declare continue handler for sqlstate '02000' set num=1; truncate table t_cell_name; -- 打開遊標 open lat_long_list; -- 將遊標中的值賦值給變量,要注意sql結果列的順序 fetch lat_long_list into n_Longitude,n_Latidute,n_cellname; -- 打印值 -- select n_Longitude,n_Latitude; -- while循環 while num<>1 do select count(*) into isExist from tableb; if isExist = 1 then set countExist = countExist +1; insert into t_cell_name(cellname) values(n_cellname); end if; -- 循環變量下一條數據,將遊標中的值賦值給變量,要注意sql結果列的順序 fetch lat_long_list into n_Longitude,n_Latitude,n_cellname; end while; -- 關閉遊標 close lat_long_list; -- 打印值 select countExist; end