MYSQL StoredProcedure插入问题

时间:2015-08-04 22:04:38

标签: mysql stored-procedures insert

我有存储过程,我想插入一个学生,插入一个监护人,然后从每个中取出ID并将记录插入一个关联表。

以下是我使用的代码,但我收到以下错误:

1064 - 您的SQL语法出错;查看与您的MySQL服务器版本相对应的手册,以便在' SET @studentId = LAST_INSERT_ID()INSERT INTO TblPersons(firstName,lastName'第40行

附近使用正确的语法

创建程序usp_InsertStudentAndGuardian
IN p_firstName VARCHAR(45),
IN p_lastName VARCHAR(45),
IN p_gender VARCHAR(1),
在p_birthDate DATE,
IN p_joinDate DATE,
IN p_isActive BIT,
在p_isUnderAge BIT,
IN p_gFirstName VARCHAR(45),
IN p_gLastName VARCHAR(45),
IN p_gGender VARCHAR(1),
IN p_gBirthDate DATE,
IN p_gJoinDate DATE,
IN p_gIsActive BIT,
IN p_gIsUnderAge BIT,
IN p_personType INT

BEGIN

插入TblPersons

firstName,
lastName,
性别,
birthDate,
joinDate,
isActive,
isUnderAge

VALUES

p_fistName,
p_lastName,
p_gender,
p_birthDate,
p_joinDate,
p_isActive,
p_isUnderAge

SET @studentId = LAST_INSERT_ID()< ------ LINE 40

插入TblPersons

firstName,
lastName,
性别,
birthDate,
joinDate,
isActive,
isUnderAge

VALUES

p_gFirstName,
p_gLastName,
p_gGender,
p_gBirthDate,
p_gJoinDate,
p_gIsActive,
p_gIsUnderAge

SET @guardianId = LAST_INSERT_ID()

插入AtbStudentsGuardians
(@studentId,@ guardianId)

INSERT INTO AtbPersonsPersonTypes
(@studentId,p_personType);

END //

1 个答案:

答案 0 :(得分:1)

使用;终止每个SQL语句,否则它将被视为单个语句,如下所示

INSERT INTO TblPersons
(
firstName,
lastName,
.......
)
VALUES
(
p_fistName,
p_lastName,
......
);
SET @studentId := LAST_INSERT_ID();

再次,我认为你的代码行在

下面
SET @studentId = LAST_INSERT_ID(); <------ LINE 40

应该是

SET @studentId := LAST_INSERT_ID();