mysql中的错误1064,有人能检查错误是什么吗?

xlpyo6sf  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(420)
CREATE PROCEDURE insert_user(in uname varchar(20),in gender varchar(20),in email varchar(20),in phone varchar(20),in pword varchar(20),in city varchar(20)) 
BEGIN
DECLARE finished integer default 0;
Declare cnt integer default 0;
declare id integer;
DECLARE  c_cur cursor for select user_id from user;
DECLARE CONTINUE HANDLE FOR NOT FOUND SET finished = 1;
    open c_cur;
        ins_user: loop
        fetch c_cur into id;

                IF finished = 1 THEN 
                LEAVE ins_user;
                end if; 

                cnt:=id;
                end loop ins_user;
        cnt:=cnt+1;
        insert into user
        values(cnt,uname,email,phone,city,pword,gender);
END;

我收到错误#1064-您的sql语法有错误;请查看与您的mysql服务器版本对应的手册,以了解使用“handle for not found set finished=1;打开CU cur;ins\u用户:在第7行循环fetch,我不知道哪里出错了

w6lpcovy

w6lpcovy1#

将分隔符设置为除 ; ,例如: $$ . 这将允许解析器处理整个 create 作为一个整体的声明。
最后,将分隔符重新定义回
; user 是mysql中的关键字。最好将表重命名为其他名称,或者在表周围使用反勾号。
有一个拼写错误;应该是的 HANDLER 不是 HANDLE 尝试:

DELIMITER $$
DROP PROCEDURE IF EXISTS insert_user $$

CREATE PROCEDURE insert_user(in uname varchar(20),
                             in gender varchar(20),
                             in email varchar(20),
                             in phone varchar(20),
                             in pword varchar(20),
                             in city varchar(20)) 
BEGIN
DECLARE finished INT DEFAULT 0;
Declare cnt INT DEFAULT 0;
declare id INT;
DECLARE c_cur CURSOR FOR select user_id from `user`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
    open c_cur;
        ins_user: loop
        fetch c_cur into id;

                IF finished = 1 THEN 
                LEAVE ins_user;
                end if; 

                SET cnt:=id; -- set was missing here
                end loop ins_user;
        SET cnt:=cnt+1; -- set was missing here

        insert into `user`
        values(cnt,uname,email,phone,city,pword,gender);
END $$

DELIMITER ;

相关问题