没有语法错误时,MySQL 1064语法错误

时间:2011-08-30 20:29:10

标签: mysql sql database stored-procedures syntax-error

为了保护我的工作,改变了一些名称和内容。

DELIMITER //
CREATE PROCEDURE glt(IN howMany INT)
BEGIN

  DECLARE f VARCHAR(32);
  DECLARE done INT DEFAULT 0;
  DECLARE curs CURSOR FOR SELECT DISTINCT id FROM tpd;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  OPEN curs;

  DROP TABLE IF EXISTS lt;
  CREATE TEMPORARY TABLE lt LIKE tpd;

  REPEAT
    FETCH curs INTO f;
    IF NOT done THEN
      INSERT INTO lt SELECT * FROM tpd WHERE id = f ORDER BY TIME DESC LIMIT howMany;
    END IF;
  UNTIL done END REPEAT;
  CLOSE curs;
END

上面的代码在linux机器上给出了以下错误,但不是mac机器,尽管它们都是区分大小写的文件系统并且具有相同的MySQL版本:

ERROR 1064 (42000) at line 172: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'howMany;
    END IF;
  UNTIL done END REPEAT;
  CLOSE curs;
END' at line 16
Bye

2 个答案:

答案 0 :(得分:1)

尝试使用Prepare Statement进行INSERT查询。 看http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html

看起来好像是:

PREPARE stmt FROM 'INSERT INTO lt SELECT * FROM tpd WHERE id = f ORDER BY TIME DESC LIMIT ?';
EXECUTE stmt USING howMany;

答案 1 :(得分:0)

您不能在limit子句中使用参数。
您只能在使用PDO时执行此操作。在这种情况下,这是一个语法错误。