我正在尝试编写一个存储过程,其中我使用2个表根据条件来获取数据,但是它给出了错误。
我的SP是:
DELIMITER //
CREATE PROCEDURE select_notifications(IN datePosted DATETIME,IN recipientId INT,IN packageId INT,IN accountId INT)
BEGIN
IF datePosted < NOW() THEN
SELECT tn.subject_line,tn.message,tn.message_type FROM track_notifications AS tn WHERE tn.package_id=packageId AND tn.recipient_id=recipientId AND tn.account_id=accountId AND tn.date_posted = datePosted;
ELSE IF datePosted > NOW()
SELECT subject_line,message,message_type FROM track_notifications_new WHERE package_id=packageId AND recipient_id=recipientId AND account_id=accountId AND date_posted = datePosted;
END IF;
END
END //
Mysql错误:
以下查询失败:“ CREATE DEFINER =
root
@localhost
程序select_track_notifications
(在datePosted
日期,INrecipientId
INT,INpackageId
INT,INaccountId
INT)不 如果日期已发布,则包含确定性SQL SQL安全定义程序 NOW()然后选择tn.subject_line,tn.message,tn.message_type FROM track_notifications AS tn在哪里tn.package_id = packageId和 tn.recipient_id = recipientId和tn.account_id = accountId和 tn.date_posted = datePosted;如果日期已发布>现在()然后选择 subject_line,message,message_type FROM track_notifications_new在哪里 package_id = packageId和收件人_id =收件人ID和 account_id = accountId AND date_posted = datePosted;万一; END”
MySQL说:
有帮助吗?谢谢。
答案 0 :(得分:1)
END
。;
。ELSEIF .... THEN
(ELSE和IF之间没有空格)。SELECT .. INTO
子句将选择查询的结果存储到变量中(您将对其进行进一步的操作)。请尝试以下操作:
DELIMITER //
DROP PROCEDURE IF EXISTS select_notifications //
CREATE PROCEDURE select_notifications(IN datePosted DATETIME,
IN recipientId INT,
IN packageId INT,
IN accountId INT)
BEGIN
IF datePosted < NOW() THEN
SELECT tn.subject_line,
tn.message,
tn.message_type
FROM track_notifications AS tn
WHERE tn.package_id=packageId AND
tn.recipient_id=recipientId AND
tn.account_id=accountId AND
tn.date_posted = datePosted;
ELSEIF datePosted > NOW() THEN
SELECT subject_line,
message,
message_type
FROM track_notifications_new
WHERE package_id=packageId AND
recipient_id=recipientId AND
account_id=accountId AND
date_posted = datePosted;
END IF;
END //
DELIMITER ;