我在MySQL中创建了一个存储例程,我想知道是否有可能使INSERT-IF某些条件为TRUE,ELSE INSERT其他条件。
这是我的查询,但我不断收到#1064错误(SQL语法错误)。
DECLARE a INT;
DECLARE b INT;
DECLARE changeTypeID INT DEFAULT(0);
SET a = (SELECT roomPrice FROM RPH WHERE tier=1 AND
startDate = '2018-02-20' AND rTypeID=1);
SET b = (SELECT roomPrice FROM RPH WHERE tier=1 AND
startDate = '2018-02-20' AND rTypeID=2);
SET changeTypeID = CASE WHEN (a>b) THEN 1 WHEN (a<b) THEN 2 WHEN a=b THEN 3 END;
IF (changeTypeID = 1 OR changeTypeID =2) THEN
(
INSERT INTO TABLE_A (X,Y,Z) VALUES (ID, changeTypeID, createdBy);
)
ELSEIF (changeTypeID = 3) THEN
(
INSERT INTO TABLE_B (P,Q,R) VALUES (ID, rID, createdBy);
)
END IF;
以下是错误消息:
SQL Error (1064): You have an error in your SQL Syntax; check the manual
that corresponds to your MySQL version for the right syntax to use near
'INSERT INTO TABLE_A (X,Y,Z)' at line 19
注意:事先已在例程中声明了insert参数。
任何帮助将不胜感激:)
答案 0 :(得分:0)
DECLARE a INT;
DECLARE b INT;
DECLARE changeTypeID INT DEFAULT(0);
SET a = (SELECT roomPrice FROM RPH WHERE tier=1 AND
startDate = '2018-02-20' AND rTypeID=1);
SET b = (SELECT roomPrice FROM RPH WHERE tier=1 AND
startDate = '2018-02-20' AND rTypeID=2);
SET changeTypeID = CASE WHEN (a>b) THEN 1 WHEN (a<b) THEN 2 WHEN a=b THEN 3 END;
IF (changeTypeID = 1 OR changeTypeID =2) THEN
INSERT INTO TABLE_A (X,Y,Z) VALUES (ID, changeTypeID, createdBy);
ELSEIF (changeTypeID = 3) THEN
INSERT INTO TABLE_B (P,Q,R) VALUES (ID, rID, createdBy);
END IF;