mysql过程语法错误
我通过发送以下查询(MYSQL 5.0)收到错误:
DELIMITER //
CREATE PROCEDURE relationTable ()
BEGIN
DECLARE articlecount int;
DECLARE keywordcount int;
DECLARE articlehits int;
DECLARE ac int DEFAULT 0;
DECLARE kc int;
DECLARE articleid int;
DECLARE word varchar(100);
DECLARE word_id int;
SET articlehits = 0;
SET articlecount = (SELECT count(id) from articles);
SET keywordcount = (SELECT count(id) from keywords);
outerloop: WHILE (ac < articlecount) DO
SET kc = 0;
SET articleid = (SELECT id from articles LIMIT 1 OFFSET ac);
innerloop: WHILE (kc < keywordcount) DO
IF (articlehits < 5) THEN
SELECT keyword, id INTO word, word_id from keywords LIMIT 1 OFFSET kc;
IF (0 < (SELECT COUNT(id) from articles WHERE id=articleid AND CONCAT(title, " ",text) REGEXP word)) THEN
INSERT INTO articles (id, articleID, keywordID, type) VALUES(NULL, articleid, word_id, 'type1');
SET articlehits = articlehits + 1;
END IF;
SET kc = kc + 1;
ELSE
SET kc = keywordcount;
END IF;
END WHILE innerloop;
SET ac = ac + 1;
END WHILE outerloop;
END;
//
DELIMITER ;
这会产生以下错误:
错误 1064 (42000):您的 SQL 语法有错误;检查与您的MySQL服务器版本对应的手册以获取正确的信息 在 'LIMIT 1 OFFSET ac) 附近使用的语法;内循环:WHILE (kc < keywordscount) DO TO word, word_id from' 第 15 行
知道为什么会发生这种情况吗?
(编写此内容是为了创建文章和关键字之间的关系表,以在文章视图中启用智能链接。)
I get an error by sending the following query (MYSQL 5.0):
DELIMITER //
CREATE PROCEDURE relationTable ()
BEGIN
DECLARE articlecount int;
DECLARE keywordcount int;
DECLARE articlehits int;
DECLARE ac int DEFAULT 0;
DECLARE kc int;
DECLARE articleid int;
DECLARE word varchar(100);
DECLARE word_id int;
SET articlehits = 0;
SET articlecount = (SELECT count(id) from articles);
SET keywordcount = (SELECT count(id) from keywords);
outerloop: WHILE (ac < articlecount) DO
SET kc = 0;
SET articleid = (SELECT id from articles LIMIT 1 OFFSET ac);
innerloop: WHILE (kc < keywordcount) DO
IF (articlehits < 5) THEN
SELECT keyword, id INTO word, word_id from keywords LIMIT 1 OFFSET kc;
IF (0 < (SELECT COUNT(id) from articles WHERE id=articleid AND CONCAT(title, " ",text) REGEXP word)) THEN
INSERT INTO articles (id, articleID, keywordID, type) VALUES(NULL, articleid, word_id, 'type1');
SET articlehits = articlehits + 1;
END IF;
SET kc = kc + 1;
ELSE
SET kc = keywordcount;
END IF;
END WHILE innerloop;
SET ac = ac + 1;
END WHILE outerloop;
END;
//
DELIMITER ;
This produces the following error:
ERROR 1064 (42000): 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 'LIMIT 1 OFFSET ac); innerloop: WHILE (kc <
keywordcount) DO TO word, word_id from' at line 15
Any idea why this happens?
(Wrote this to create a relation table between articles and keywords, to enable smart-links in article view.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试删除 while 标签:
Try to remove the while labels:
MySQL 具有 @varname 形式的特定于连接的用户定义变量以及您在代码示例中使用的类型的声明过程变量。根据我的程序经验,有时只允许其中一种类型,如果我没记错的话,其中一种情况可能是在 SELECTing INTO 时。您可以在这里尝试使用用户定义的变量,如下所示:
只是一个想法。
MySQL has connection-specific user-defined variables of the form @varname and declared procedure variables of the type you are using in your code sample. In my experience with procedures, sometimes only one of the types is allowed, and if I recall correctly, one of those situations may be when SELECTing INTO. You might try using user-defined variables here, as follows:
Just an idea.
一个问题可能是 SET acticleid = (SELECT...)。尝试使用
SELECT .. INTO
:存储过程中的变量
LIMIT
仅在新的MySQL版本中受支持。请注意,由于您没有ORDER BY
,您将得到一个随机行。看起来您想改用CURSOR
。请参阅文档。One problem may be the SET acticleid = (SELECT...). Try with
SELECT .. INTO
:Variable
LIMIT
in stored procedures is only supported in new MySQL versions. Note that since you dont have anORDER BY
you will get a random row. It looks like you want to use aCURSOR
instead. See docs.冒着显得过于挑剔的风险,我相信您应该重写此过程以使用游标遍历数据库,而不是使用 LIMIT 进行单独选择。
请参阅 MySQL 文档中的游标
At the risk of seeming over-critical, I believe you should rewrite this procedure to use cursors for traversing your databases, instead of individual selects with LIMIT.
see Cursors in MySQL Docs
感谢您迄今为止的帮助。
事实证明,汤姆·霍斯的想法是正确的。 SELECT INTO 语句的变量必须是用户定义的。
我编辑了代码以使用游标和用户定义的变量,如下所示:
现在我收到另一个我无法解释的错误:
这个错误让我很困惑,因为处理程序不可能有问题。处理程序的声明如 mysql 文档的 示例 中所示。问题可能是我无法像这样创建两个光标吗?
Thank for your help so far.
The idea of Tom Haws proved to be correct. The variables for a SELECT INTO statement have to be user-defined.
I edited my code to use cursors, and user-defined variables as followed:
And now I get an other error that I really can't explain:
This error confuses me because it can't have a problem with the handler. The handler is declared as in the example of the mysql-documentation. Could the problem be that I can't create two cursors like this?