mysql存储过程(带2个插入)错误

时间:2012-04-01 13:48:25

标签: mysql

我有这张桌子

create table eveniment( +
       evenimentId bigint not null auto_increment primary key,
       evenimentDenumire varchar(500),
       adresaId int not null);

create table adresa(
       adresaId bigint not null auto_increment primary key,
       localitate varchar(500),
       judet varchar(500),
       codPostal varchar(50),
       strada varchar(500),
       nr varchar(50),
       bl varchar(50),
       ap varchar(5),
       email varchar(500),
       www varchar(500));

和这个程序

DELIMITER //
drop procedure IF EXISTS insertEveniment;
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0 
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www) 
    values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
DECLARE vEvenimentId int DEFAULT 0 
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();

我收到此错误

  

错误1064(42000):您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)值附近使用正确的语法('在第4行

如果我在mysql命令中单独尝试插入adresa ... 我不会收到错误,我做错了什么?

2 个答案:

答案 0 :(得分:1)

尝试这样(尝试过,它对我有用):

drop procedure IF EXISTS insertEveniment;
DELIMITER //
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0;
DECLARE vEvenimentId int DEFAULT 0;
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www) 
    values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();

您正在使用带有错误分隔符的drop procedure语句(您将分隔符设置为//然后放置;在语句结尾处。在设置分隔符之前放置drop procedure语句,并且它会工作得很好。另外,在DECLARE之后放置分隔符

答案 1 :(得分:0)

DECLARE是一个单独的声明,因此需要使用;分隔,例如INSERTSELECT或任何其他声明。

此外,将第一个DELIMITER语句移到过程声明开始之前。

所以,

DELIMITER //
drop procedure IF EXISTS insertEveniment;
DELIMITER //
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0 ;
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www) 
    values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
DECLARE vEvenimentId int DEFAULT 0 ;
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();