MySQL Forums
Forum List  »  Stored Procedures

Always returns TRUE
Posted by: Jimmy Tagole
Date: July 08, 2020 04:53AM

CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `InsertTransaction`(IN retnum VARCHAR(11), IN amount DECIMAL(19,2), IN dspid INT)
InsertTransaction:BEGIN

DECLARE serialidx INT DEFAULT 0;
DECLARE arcounter INT DEFAULT 0;
DECLARE arnumber VARCHAR(255) DEFAULT NULL;
DECLARE batchidx VARCHAR(255) DEFAULT NULL;
DECLARE retname VARCHAR(255) DEFAULT NULL;

/* Check if there is serial available */
IF NOT EXISTS (SELECT * FROM `nowsms`.`amax_serial` WHERE `InUse` = 1 AND `DSPid` = dspid) THEN
SELECT '-2000' AS Result;
LEAVE InsertTransaction;
END IF;

/* Check for double loading */
IF EXISTS (SELECT * FROM `nowsms`.`amax_transactions` WHERE `Amount` = amount) THEN
SELECT '-2001' AS Result;
LEAVE InsertTransaction;
END IF;

/* Check if retailer has alias and use alias if available */
IF EXISTS (SELECT * FROM `nowsms`.`amax_retailers` WHERE `Number` = retnum) THEN
IF (SELECT `Alias` FROM `nowsms`.`amax_retailers` WHERE `Number` = retnum) IS NOT NULL THEN
SELECT `Alias` INTO retname FROM `nowsms`.`amax_retailers` WHERE `Number` = retnum;
END IF;
END IF;

/* Get info from serial table */
SELECT `ArNum`, `Counter`, `BatchID`, `ID`
INTO arnumber, arcounter, batchidx, serialidx
FROM `nowsms`.`amax_serial`
WHERE `InUse` = 1 AND `DSPid` = dspid;

/* Insert transaction */
INSERT INTO `nowsms`.`amax_transactions` (`ARnum`, `Name`, `Number`, `Amount`, `Type`, `BatchID`, `DSPid`)
VALUES (CONCAT(arnumber, arcounter), retname, retnum, amount, 'DLOAD 1', batchidx, dspid);

/* Update serial counter */
UPDATE `nowsms`.`amax_serial` SET `Counter` = `Counter` + 1 WHERE `ID` = serialidx;

/* OK Result */
SELECT CONCAT(arnumber, arcounter) AS Result;
END


Why this "IF EXISTS (SELECT * FROM `nowsms`.`amax_transactions` WHERE `Amount` = amount)" always returns TRUE even without no existing same amount in the table?


MySQL newbie here...

Options: ReplyQuote


Subject
Views
Written By
Posted
Always returns TRUE
368
July 08, 2020 04:53AM
158
July 08, 2020 10:15AM
196
July 08, 2020 11:28AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.