CREATE TABLE t1 (s1 INT, PRIMARY KEY (s1));
DELIMITER ;
CREATE PROCEDURE handlerdemo ()
BEGIN
DECLARE x INTEGER;
SET @x = 1;
INSERT INTO t1 VALUES (1);
SET @x = 2;
INSERT INTO t1 VALUES (1);
SET @x = 3;
END;
当我运行此查询时,我在第 4 行收到 1064 错误。 非常感谢任何关于如何处理它的提示。
最佳答案
- 您需要将 Delimiter 设置为任何except
;
- 可选地,检查是否已存在同名存储过程。
- 最后,将分隔符重新定义回
;
- 除非,您打算在此存储过程之外使用变量
x
;你真的不需要使用@
;它使该变量在该特定 session 中随处可用)。
尝试(更多解释在评论中):
CREATE TABLE t1 (s1 INT, PRIMARY KEY (s1)); -- create the table
DELIMITER $$ -- redefine the delimiter to $$ (for eg)
DROP PROCEDURE IF EXISTS `handlerdemo` $$ -- drop previous if exists
CREATE PROCEDURE handlerdemo ()
BEGIN
DECLARE x INT DEFAULT 0; -- datatype is INT
-- also a good practice to set default value
SET x = 1; -- no need to use in Session context
INSERT INTO t1 VALUES (x); -- use variable name here instead of literal value
SET x = 2;
INSERT INTO t1 VALUES (x);
END $$ -- remember that delimiter is $$ right now
-- redefine the Delimiter back to ;
DELIMITER ;
关于mysql - 我无法弄清楚我在进行此 MYSQL 查询时犯了什么错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52886160/