在mysql中的row_count> 0之后循环到下一次更新

时间:2014-03-25 09:14:06

标签: mysql stored-procedures

我在mysql中有以下程序

CREATE PROCEDURE accidents.`task_6_proce`()
BEGIN

  DECLARE v_finished INTEGER DEFAULT 0;
  DECLARE v_pkid INTEGER;
  DECLARE v_mine INTEGER;
  DECLARE v_date DATETIME;
  DECLARE v_lat INTEGER;
  DECLARE v_lng INTEGER;
  DECLARE v_next_date DATE;
  DECLARE i INTEGER DEFAULT 0;

DECLARE weather_cursor CURSOR FOR select pkid, mine_id, accident_dt, lat, lng
                              from test_unfilteredfull
                              where pkid between 1 and 100;

-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER 
    FOR NOT FOUND SET v_finished = 1;
OPEN weather_cursor;

get_update: LOOP

    FETCH weather_cursor INTO v_pkid, v_mine, v_date, v_lat, v_lng;

    SET v_next_date = DATE_ADD(v_date,INTERVAL 1 DAY);
    -- select v_date, v_next_date;

    IF v_finished = 1 THEN 
        LEAVE get_update;
    END IF;
last_update: loop  
      UPDATE mytable mt INNER JOIN myvalues mv
         ON mv.date = v_next_date and
         mt.pkid = v_pkid 
      SET mt.tmax = mv.tmax, mt.tmin = mv.tmin, mt.dayl= mv.dayl ,
          mt.prcp = mv.prcp , mt.srad = mv.srad, mt.swe=mv.swe, mt.vp=mv.vp
    WHERE mv.Longitude_MAX >= v_lng and 
      mv.Longitude_MIN <= v_lng and 
      mv.Latitude_MAX >=  v_lat and  
      mv.Latitude_MIN <= v_lat;
if ROW_COUNT() > 0 then
   LEAVE last_update;
end if;

IF v_next_date = LAST_DAY(v_next_date) then
   LEAVE last_update;
end if;

  SET v_next_date = DATE_ADD(v_next_date,INTERVAL 1 DAY);
  SET i = i+1;
end LOOP last_update;
END LOOP get_update;

CLOSE weather_cursor;
commit;  

END;

这里的问题 if ROW_COUNT() > 0 then LEAVE last_update; end if; 当第一条记录更新时,它会离开循环并结束程序。如何完成该结束但仅针对该记录并开始为光标中的下一个pkid?

0 个答案:

没有答案