mysql变量声明语法错误

时间:2021-05-22 23:11:50
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 - SQL语法有错误;检查对应于>的MySQL服务器版本的手册,在第3行使用正确的语法

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

但对我来说,一切似乎都是对的。我真的不知道!有人能帮忙吗?

thanks

谢谢

2 个解决方案

#1


40  

You need to temporarily change the delimiter so 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 ;

#2


-2  

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

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

SET @x = 0;

Also, variables need to be prefixed with the @ symbol

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

#1


40  

You need to temporarily change the delimiter so 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 ;

#2


-2  

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

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

SET @x = 0;

Also, variables need to be prefixed with the @ symbol

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