mardi 27 février 2018

MySQL INSERT IF condition is true

I'm making a stored routine in MySQL, and I would like to know whether it is possible to make an INSERT-IF some condition is TRUE, ELSE INSERT something else.

This is My Query but I keep getting the #1064 Error (SQL Syntax Error)

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 typeID = CASE WHEN (a>b) THEN 1 WHEN (a<b) THEN 2 WHEN a=b THEN 3 END;
IF (typeID = 1 OR typeID =2) THEN
(
INSERT INTO TABLE_A (X,Y,Z) VALUES (ID, changeTypeID, createdBy)
)
ELSEIF (typeID = 3) THEN
(
INSERT INTO TABLE_B (P,Q,R) VALUES (ID, rID, createdBy);
)
END IF;

Note: The insert parameter is already declared on the routine beforehand.

Any help will be appreciated :)

Aucun commentaire:

Enregistrer un commentaire