我在INSERT INTO周围遇到语法错误,我似乎无法摆脱除了MySQL文档之外我所要求的其他开发人员之间的问题。如果有任何帮助,我将不胜感激。
CREATE PROCEDURE teacher_insert(
IN p_id int,
IN p_dept int,
IN p_name varchar(50),
IN p_phone varchar(50),
IN p_mobile varchar(50)
)
BEGIN
IF EXISTS(SELECT * FROM teacher WHERE id = p_id) THEN
BEGIN
UPDATE teacher
SET dept = p_dept,
name = p_name,
phone = p_phone,
mobile = p_mobile
WHERE id = p_id
END
ELSE
BEGIN
INSERT INTO teacher (id,dept,name,phone,mobile)
VALUES(p_id,p_dept,p_name,p_phone,p_mobile)
END
END
- 在程序的每个语句后面都需要
;
- 你不把
SET
放在INSERT
的列列表之前。
但是您做了很多额外的工作,因为您可以使用单个INSERT
与ON DUPLICATE KEY
而不是使用IF
检查键(假设id
列是唯一键)。
CREATE PROCEDURE teacher_insert(
IN p_id int,
IN p_dept int,
IN p_name varchar(50),
IN p_phone varchar(50),
IN p_mobile varchar(50)
)
INSERT INTO teacher (id,dept,name,phone,mobile)
VALUES (p_id,p_dept,p_name,p_phone,p_mobile)
ON DUPLICATE KEY UPDATE
dept = VALUES(dept),
name = VALUES(name),
phone = VALUES(phone),
mobile = VALUES(mobile)
;