我在 MYSQL 数据库上练习这个存储过程来在账户之间进行支付。它接受两个帐户(acct1,acct2)的输入,要支付的金额(amt)并输出确认消息(pmessage)
它应该只在以下情况下付款:(IF (balance1 >=amt) THEN ...),但是当我输入任何大于可用余额的金额时,它仍然会继续付款。我不明白。请帮忙。
这是代码:
DELIMITER //
CREATE PROCEDURE `make_payment`(IN `acct1` int(4), IN `acct2` int(4), IN `amt` float(10,2) unsigned, OUT `pmessage` varchar(100))
BEGIN
DECLARE balance1 FLOAT;
DECLARE balance2 FLOAT;
IF(acct1 !=acct2) THEN
SELECT balance INTO balance1 FROM mydb.accounts
WHERE account_no =acct1
ORDER BY balance DESC LIMIT 1;
SELECT balance INTO balance2 FROM mydb.accounts
WHERE account_no =acct2
ORDER BY balance DESC LIMIT 1;
IF (balance1 >=amt) THEN
begin
SET balance1 =balance1-amt;
SET balance2 =balance2+amt;
INSERT INTO mydb.accounts(account_no, outflow, balance)
VALUES(acct1, amt, balance1);
INSERT INTO mydb.accounts(account_no, inflow, balance)
VALUES(acct2, amt, balance2);
SET pmessage ="payment was successful";
END;
ELSE SET pmessage ="insufficient balance";
END IF;
END IF;
END //
DELIMITER ;