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

发布于 2024-12-02 07:44:29 字数 902 浏览 0 评论 0原文

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

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

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 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶葆Ⅱㄣ 2024-12-09 07:44:29

尝试使用Prepare语句进行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;

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;
谈下烟灰 2024-12-09 07:44:29

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文