mysql 預存程序使用遊標執行個體

來源:互聯網
上載者:User

 

使用了mysql預存程序、暫存資料表、遊標

CREATE DEFINER=`root`@`%` PROCEDURE `get_driver_nearconsumer`(    in_groupid int,    in_serviceid int)BEGINdeclare temp_id int;declare temp_x decimal(10,6);declare temp_y decimal(10,6);declare temp_c_x decimal(10,6);declare temp_c_y decimal(10,6);
DECLARE done bool default FALSE;
DECLARE cur1 CURSOR FOR SELECT int_driver,dec_x,dec_y FROM ( select d.*, a.vc_name as grouparea_name , g.dec_x,g.dec_y,g.dt_sc ,c.dcount,dd.consumer_name,dd.vc_fadd,dd.vc_tadd,dd.int_service,dd.int_status from bi_driver d left join bi_gisdata g on g.int_mubiao = d.int_driver and g.int_type = 1 left join bi_grouparea a on a.int_grouparea = d.int_area left join (select count(1) as dcount,int_driver from bi_serviceinfo where int_status >-1 and int_status < 100 group by int_driver ) c on c.int_driver = d.int_driver left join (select int_service,consumer_name,vc_fadd,vc_tadd,int_driver,int_status from v_serviceinfo where int_service in (select int_service from bi_serviceinfo where int_status >-1 and int_status < 100 and ifnull(int_driver,0)>0 group by int_driver) ) dd on dd.int_driver = d.int_driver where int_area = in_groupid and g.dt_sc>DATE_ADD(CURRENT_TIMESTAMP,INTERVAL -1 HOUR) ) cur_table;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = true;#結束標識
DROP TABLE IF EXISTS r_table ; CREATE TEMPORARY TABLE r_table select d.*, a.vc_name as grouparea_name , g.dec_x,g.dec_y,g.dt_sc ,c.dcount,dd.consumer_name,dd.vc_fadd,dd.vc_tadd,dd.int_service,dd.int_status ,g.dec_x as distance#添加一列記錄距離from bi_driver d left join bi_gisdata gon g.int_mubiao = d.int_driver and g.int_type = 1left join bi_grouparea a on a.int_grouparea = d.int_arealeft join (select count(1) as dcount,int_driver from bi_serviceinfo where int_status >-1 and int_status < 100 group by int_driver) con c.int_driver = d.int_driver left join (select int_service,consumer_name,vc_fadd,vc_tadd,int_driver,int_status from v_serviceinfo where int_service in (select int_service from bi_serviceinfo where int_status >-1 and int_status < 100 and ifnull(int_driver,0)>0 group by int_driver)) ddon dd.int_driver = d.int_driver where int_area = in_groupid and g.dt_sc>DATE_ADD(CURRENT_TIMESTAMP,INTERVAL -1 Hour) ;
#delete from r_table;set temp_c_x =(select dec_fx from bi_serviceinfo where int_service = in_serviceid );set temp_c_y =(select dec_fy from bi_serviceinfo where int_service = in_serviceid);#select temp_c_x,temp_c_y;
OPEN cur1;fetchSeqLoop:LoopFETCH cur1 INTO temp_id,temp_x,temp_y;if done then leave fetchSeqLoop; else #select temp_id,temp_x,temp_y; update r_table set distance = f_getdistance(temp_c_x,temp_c_y,temp_x,temp_y) where int_driver = temp_id;end if; end Loop; CLOSE cur1;
select * from r_table where distance < 2000 order by distance;drop table r_table;END
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.