开发者

MySQL 1064 Syntax error when there isn't a syntax error

开发者 https://www.devze.com 2023-04-01 01:28 出处:网络
Some names and stuff changed to protect my work. DELIMITER // CREATE PROCEDURE glt(IN howMany INT) BEGIN

Some names and stuff changed to protect my work.

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 HAN开发者_JS百科DLER 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

The above code gives the following error on a linux machine, but not a mac machine despite both being case-sensitive filesystems and having the same MySQL version:

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


Try to use Prepare Statement for INSERT query. Look at http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html

It seems it will look like:

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


You cannot use a parameter in the limit clause here.
You can only do that when using PDO. In this context that's a syntax error.

0

精彩评论

暂无评论...
验证码 换一张
取 消