0

我正在尝试在 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,UserID_FOR)
select txt, friends.UserID,friends.FriendID
from friends where friends.UserID = BY and friends.FriendID = FORR;
end//
delimiter;

什么是问题?

4

1 回答 1

1

您不应该使用保留关键字。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 ;

另一个问题是你end ifif-else陈述中没有。

于 2013-09-20T13:13:20.323 回答