mysql存储过程的语法
我试图在MySQL中创建一个存储过程,但它给了我一个语法错误 以下是查询:mysql存储过程的语法,mysql,sql,stored-procedures,Mysql,Sql,Stored Procedures,我试图在MySQL中创建一个存储过程,但它给了我一个语法错误 以下是查询: delimiter // create procedure PostToTimeline (in txt text, in BY int, in FORR int) begin if BY = FORR then insert into posts (Body,UserID_BY,UserID_FOR) values(txt,BY,FORR); else insert into posts (Body,UserID_BY
delimiter //
create procedure PostToTimeline (in txt text, in BY int, in FORR int)
begin if BY = FORR then
insert into posts (Body,UserID_BY,UserID_FOR) values(txt,BY,FORR);
else
insert into posts (Body,UserID_BY,UserID_FOR)
select txt, friends.UserID,friends.FriendID
from friends where friends.UserID = BY and friends.FriendID = FORR;
end//
delimiter;
问题是什么 您不应该使用保留关键字
BY
是保留关键字。最好的解决办法是改变它,而不是逃避它
DELIMITER //
CREATE PROCEDURE PostToTimeline
(
IN txt TEXT,
IN BYParam INT,
IN FORR INT
)
BEGIN
IF BYParam = FORR THEN
INSERT INTO posts (Body, UserID_BY, UserID_FOR)
VALUES(txt, BYParam, FORR);
ELSE
INSERT INTO posts (Body, UserID_BY, UserID_FOR)
SELECT txt, friends.UserID, friends.FriendID
FROM friends
WHERE friends.UserID = BYParam AND
friends.FriendID = FORR;
END IF;
END//
DELIMITER ;
另一个问题是,您的
if else
语句中没有end if
。您的新代码给了我错误#1064-您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解使用PROPCEDURE PostToTimeline(在txt文本中,在BYParam INT中,在F'中,在第1行<代码>过程非<代码>PROPCEDURE)附近的正确语法。很抱歉输入错误。您遇到了什么错误?