mysql 变量声明的语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7408088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:03:39  来源:igfitidea点击:

syntax error for mysql declaration of variable

mysqlsyntax

提问by moris

CREATE PROCEDURE dorepeat(IN p1 INT)
BEGIN
  DECLARE x INT DEFAULT 0;
  REPEAT SET x = x + 1; UNTIL x > p1 END REPEAT;
END

I get an syntax error:

我收到一个语法错误:

#1064 - 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 '' at line 3

#1064 - 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 '' at line 3

But for me, everything seems to be correct. i really don't have any clue! can anybody help?

但对我来说,一切似乎都是正确的。我真的一点头绪都没有!有人可以帮忙吗?

thanks

谢谢

回答by John Flatness

You need to temporarily change the delimiterso the MySQL client doesn't think you're done with your statement when it sees the semicolon on line 3:

您需要临时更改分隔符,以便 MySQL 客户端在看到第 3 行的分号时不会认为您已经完成了您的语句:

DELIMITER //

CREATE PROCEDURE dorepeat(IN p1 INT)
BEGIN
  DECLARE x INT DEFAULT 0;
  REPEAT SET x = x + 1; UNTIL x > p1 END REPEAT;
END//

DELIMITER ;

回答by Derek Kromm

Remove the DECLARE, you should be able to just do this:

删除 DECLARE,您应该可以这样做:

SET @x = 0;

Also, variables need to be prefixed with the @ symbol

此外,变量需要以@符号为前缀