0

我从命令行将此代码作为 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
4

2 回答 2

3

;字符是默认分隔符,所以当 MySQL 看到第一个时;,它认为你已经完成了。创建存储过程时,需要声明不同的分隔符,如下所示:

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,将 sproc 添加到 sproc 定义也是一个好主意。

于 2012-12-17T15:52:35.903 回答
0

您是否尝试过将语句放在语句delimiter //之前CREATE并用 更改最后一行END //

于 2012-12-17T15:54:17.393 回答