【发布时间】:2014-10-20 06:31:40
【问题描述】:
我正在尝试在 MySQL (MariaDB 5.5.39) 中运行 SQL 查询
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
SET v1 = v1 - 1;
END WHILE;
END;
但是当我运行该查询时,我收到以下错误
MariaDB [elis27rel]> source /home/vagrant/test.sql;
--------------
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5
--------------
ERROR 1064 (42000) at line 1 in file: '/home/vagrant/test.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near
'' at line 3
--------------
WHILE v1 > 0 DO
SET v1 = v1 - 1
--------------
ERROR 1064 (42000) at line 5 in file: '/home/vagrant/test.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near
'WHILE v1 > 0 DO
SET v1 = v1 - 1' at line 1
--------------
END WHILE
--------------
ERROR 1064 (42000) at line 7 in file: '/home/vagrant/test.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near
'END WHILE' at line 1
--------------
END
--------------
ERROR 1064 (42000) at line 8 in file: '/home/vagrant/test.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near
'END' at line 1
我不知道我的代码有什么问题。我从 mysql 网站http://dev.mysql.com/doc/refman/5.0/en/while.html 复制并粘贴了该代码,并希望使用该脚本来弄湿我的脚。任何帮助将不胜感激。
【问题讨论】:
-
该示例代码在各行末尾都有分号。您需要在存储过程之前(和之后)使用
delimiter语句,例如delimiter $$ . . . <stored procedure> . . . delimiter ;。 -
在 SQL 文件中?或者作为mysql中的单独命令?你能用我的查询给我看一个例子吗?