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错误。 任何有关如何处理它的提示都将受到高度赞赏。
答案 0 :(得分:1)
;
;
x
,否则,请参见表。您确实不需要使用@
;它使该变量在该特定会话中的任何地方都可用。尝试(在评论中有更多解释):
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 ;
答案 1 :(得分:-1)
您只需要删除; (分号)在DELIMITER之后可以正常运行
CREATE TABLE t1 (s1 INT, PRIMARY KEY (s1));
DELIMITER -- just remove this ; from your query it will work fine
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;