在MySQL中,我无法在循环后执行命令

时间:2017-08-14 16:43:49

标签: mysql cursor

我正在使用游标和循环来对表的每个项执行存储过程。我需要每天做一次。该过程在表的每一行上工作并执行每日过程。这部分工作正常。但是,当我尝试在最后添加一个日志记录(用这个执行所花费的时间更新表RollupControl)时,这个命令根本没有执行... 我添加了两个调试选择,一个在循环内,另一个在外面。内部的一个被执行但外面的那个没有...

DROP procedure IF EXISTS `RunX`;
DELIMITER $$
CREATE PROCEDURE `RunX`()
RunX:BEGIN
    -- Roda Rup366 uma vez para cada Dispositivo
  DECLARE t0 DATETIME(2) DEFAULT NOW(2);
  DECLARE t0i FLOAT DEFAULT 0;
  DECLARE debug INT DEFAULT 1;

  DECLARE viddisp varchar(6);
  DECLARE done BOOLEAN DEFAULT FALSE;
  DECLARE _id BIGINT UNSIGNED;

  DECLARE cur CURSOR FOR (SELECT ID FROM TableM m JOIN TableR r on m.ID = r.ID 
    WHERE TIME_TO_SEC(TIMEDIFF(LastUpdatedTime,LastQueryedTime))/(60*60*24) > 1);
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;

  OPEN cur;
      RunXLoop: LOOP
        FETCH cur INTO _id;
        IF done THEN
          LEAVE RunX;
        END IF;
        SET viddisp = (SELECT IDDisp FROM TableM WHERE ID = _id);
        IF debug=1 THEN
          SELECT viddisp;
        END IF;
        CALL RunOnceADayProc(viddisp);
      END LOOP RunXLoop;
    IF debug=1 THEN
        SELECT t0i;
    END IF;  
    SET t0i = (SELECT TIMESTAMPDIFF(MICROSECOND,t0,NOW(2))/1000000);
    UPDATE RollupControl SET DH_Op = NOW(), ET = t0i WHERE ID = 366;
  CLOSE cur;  

END$$
DELIMITER ;

1 个答案:

答案 0 :(得分:1)

我在想

LEAVE RunX;

应该是

LEAVE RunXLoop;