Mysql过程遇到语法错误

时间:2019-07-15 10:38:43

标签: mysql syntax procedure

DROP PROCEDURE IF EXISTS oneTimeExcution;

CREATE PROCEDURE `oneTimeExcution`()

begin


   DECLARE done INT DEFAULT FALSE;


   DECLARE totalCount int(10) default 0;

   DECLARE i INT DEFAULT 0;


   DECLARE nowUnixTime int(10) unsigned default 0;

   DECLARE id varchar(100) default null;

   DECLARE cur CURSOR FOR SELECT deviceId FROM tb_sensor;



   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;


   select (UNIX_TIMESTAMP()-31622400) into nowUnixTime;

   select count(*) from tb_sensor into totalCount;


   OPEN cur;

             FETCH NEXT FROM cur INTO id;

   close cur;

   read_loop: loop

   IF done then

   LEAVE read_loop;

   END IF;

   END LOOP;

end;

我目前在end ifend loop中遇到语法错误。我不知道为什么。请..请让我知道问题出在哪里。
我当前的MySQL版本是5.6。

1 个答案:

答案 0 :(得分:0)

尝试以下代码

CREATE PROCEDURE `oneTimeExcution`()

begin


   DECLARE done INT DEFAULT FALSE;


   DECLARE totalCount int(10) default 0;

   DECLARE i INT DEFAULT 0;


   DECLARE nowUnixTime int(10) unsigned default 0;

   DECLARE id varchar(100) default null;

   DECLARE cur CURSOR FOR SELECT deviceId FROM tb_sensor;



   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;


   select (UNIX_TIMESTAMP()-31622400) into nowUnixTime;

   select count(*) from tb_sensor into totalCount;


   OPEN cur;

   read_loop: loop
             FETCH NEXT FROM cur INTO id;

   IF done then

   LEAVE read_loop;

   END IF;

select 'success'; # condition comes here

   END LOOP;
   close cur;
end;