这是我第一次学习存储过程。我在这里:
DELIMETER //
DROP PROCEDURE IF EXISTS RepeatTomProc //
CREATE PROCEDURE RepeatTomProc()
BEGIN
DECLARE x INTEGER;
SET x = 1;
REPEAT
INSERT INTO TOM VALUES(x);
SET x = x + 1;
UNTIL x > 100
END REPEAT;
END //
DELIMITER;
我想要发生的是,当循环尚未完成时,它会将x
值插入表TOM
。
目前表TOM
为空。
:
CREATE TABLE TOM (
TOM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (TOM_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我的问题是当我尝试通过复制和粘贴我的mysql终端中的代码来创建存储过程时出现以下错误:
mysql> DELIMETER $$
-> DROP PROCEDURE IF EXISTS RepeatTomProc $$
-> CREATE PROCEDURE RepeatTomProc()
-> BEGIN
-> DECLARE x INT;
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 'DELIMETER $$
DROP PROCEDURE IF EXISTS RepeatTomProc $$
CREATE PROCEDURE RepeatTo' at line 1
mysql> SET x = 1;
ERROR 1193 (HY000): Unknown system variable 'x'
mysql> REPEAT
->
Display all 748 possibilities? (y or n)
? DESC ITERATE ORD
// It displays lots of other possibilities which I have ommitted
-> = x + 1;
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 'REPEAT
= x + 1' at line 1
mysql> UNTIL x > 100
-> END REPEAT;
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 'UNTIL x > 100
END REPEAT' at line 1
mysql> END $$
-> DELIMITER;
我的代码来自:Loop in Stored Procedures
答案 0 :(得分:3)
您错误输入了DELIMITER
试试这个
DELIMITER //
DROP PROCEDURE IF EXISTS RepeatTomProc //
CREATE PROCEDURE RepeatTomProc()
BEGIN
DECLARE x INTEGER;
SET x = 1;
REPEAT
INSERT INTO TOM VALUES(x);
SET x = x + 1;
UNTIL x > 100
END REPEAT;
END //
DELIMITER ;
此外,您最后需要DELIMITER
和;
之间的空格