存储过程中的Mysql语法错误

时间:2012-12-17 15:48:06

标签: mysql sql stored-procedures

我从命令行将此代码作为sql脚本运行,并且我得到“你的SQL语法中有错误”几乎在所有行中!有什么想法吗?

CREATE PROCEDURE updatemandate()
 BEGIN
   DECLARE _mandate_id BIGINT(20);
   DECLARE _has_succesful_payment tinyint(1);
   DECLARE done INT DEFAULT 0;
   DECLARE cnt INT;
   DECLARE mandateCursor CURSOR FOR Select mandate_id, has_succesful_payment From mandates;
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; 
   OPEN mandateCursor;
   allmandates: LOOP
   Fetch mandateCursor INTO _mandate_id, _has_succesful_payment;
   IF done THEN LEAVE allmandates;
   END IF;
    Select COUNT(*) FROM payments WHERE mandate_id=_mandate_id AND status='OK' into cnt; 
            IF cnt>0 THEN
            SET _has_succesful_payment=1;
            END IF;

   END LOOP allmandates;
   CLOSE mandateCursor;
 END

2 个答案:

答案 0 :(得分:3)

;字符是默认分隔符,因此当MySQL看到第一个;它认为你完成了。创建sproc时,需要声明一个不同的分隔符,如下所示:

DELIMITER $$

CREATE PROCEDURE updatemandate()
READS SQL DATA
 BEGIN
   DECLARE _mandate_id BIGINT(20);
   DECLARE _has_succesful_payment tinyint(1);
   DECLARE done INT DEFAULT 0;
   DECLARE cnt INT;
   DECLARE mandateCursor CURSOR FOR Select mandate_id, has_succesful_payment From mandates;
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; 
   OPEN mandateCursor;
   allmandates: LOOP
   Fetch mandateCursor INTO _mandate_id, _has_succesful_payment;
   IF done THEN LEAVE allmandates;
   END IF;
    Select COUNT(*) FROM payment WHERE mandate_id=_mandate_id AND status='OK' into cnt; 
            IF cnt>0 THEN
            SET _has_succesful_payment=1;
            END IF;

   END LOOP allmandates;
   CLOSE mandateCursor;
 END$$

DELIMITER ;

最好将READS SQL DATA添加到细分定义in case you need to support binary logging

答案 1 :(得分:0)

您是否尝试在delimiter //语句前添加CREATE,并使用END //更改最后一行?