我尝试在mysql中创建一个过程,但是我得到一个语法错误,我不知道为什么我得到它以及''
# 1064 - You have an error in the RSQL syntax next to '' on line 4
我的程序:
CREATE PROCEDURE MUWAP_VipAdd(IN szCharName varchar(10),IN DayAdd int,IN VipType smallint,IN datenow_srv datetime)
BEGIN
IF NOT EXISTS (SELECT 1 FROM `T_VIPList` WHERE AccountID = szCharName) THEN
INSERT INTO `T_VIPList` (`AccountID`,`Date`,`Type`) VALUES (szCharName,DATE_ADD(datenow_srv, INTERVAL DayAdd DAY),VipType);
ELSE IF EXISTS (SELECT 1 FROM `T_VIPList` WHERE AccountID = szCharName AND Date > datenow_srv) THEN
UPDATE `T_VIPList` SET `Date` = DATE_ADD(`Date`,INTERVAL DayAdd DAY) WHERE `AccountID` = szCharName;
UPDATE `T_VIPList` SET `Type` = VipType WHERE `AccountID` = szCharName AND `Type` < VipType;
ELSE
UPDATE `T_VIPList` SET `Date` = DATE_ADD(`Date`,INTERVAL DayAdd DAY), `Type` = VipType WHERE `AccountID` = szCharName;
END IF
END
谢谢!
答案 0 :(得分:1)
要在THEN
语句中包含多个语句,您需要使用BEGIN...END
。
CREATE PROCEDURE MUWAP_VipAdd(IN szCharName varchar(10),IN DayAdd int,IN VipType smallint,IN datenow_srv datetime)
BEGIN
IF NOT EXISTS (SELECT 1 FROM `T_VIPList` WHERE AccountID = szCharName)
THEN INSERT INTO `T_VIPList` (`AccountID`,`Date`,`Type`) VALUES (szCharName,DATE_ADD(datenow_srv, INTERVAL DayAdd DAY),VipType);
ELSE IF EXISTS (SELECT 1 FROM `T_VIPList` WHERE AccountID = szCharName AND Date > datenow_srv)
THEN BEGIN
UPDATE `T_VIPList` SET `Date` = DATE_ADD(`Date`,INTERVAL DayAdd DAY) WHERE `AccountID` = szCharName;
UPDATE `T_VIPList` SET `Type` = VipType WHERE `AccountID` = szCharName AND `Type` < VipType;
END
ELSE UPDATE `T_VIPList` SET `Date` = DATE_ADD(`Date`,INTERVAL DayAdd DAY), `Type` = VipType WHERE `AccountID` = szCharName;
END IF
END
您还可以将两个UPDATE
查询合并到一个查询中:
UPDATE T_VIPList
SET Date = DATE_ADD(Date, INTERVAL DayAdd DAY),
Type = GREATEST(VipType, Type)
WHERE AccountID = szCharName;
事实上,似乎可以使用单个INSERT ... ON DUPLICATE KEY UPDATE
查询来完成整个事情:
INSERT INTO T_VIPList (AccountID, Date, Type)
VALUES (szCharName,DATE_ADD(datenow_srv, INTERVAL DayAdd DAY),VipType)
ON DUPLICATE KEY UPDATE
Date = IF(Date > datenow_srv, VALUES(Date), Date),
Type = GREATEST(Type, VALUES(Type));