mysql创建存储过程transfer,并调用该存储过程,模拟银行从outAccount账号转账1000元到inAccount账号。...
时间: 2023-07-07 20:35:04 浏览: 106
Node.js中调用mysql存储过程示例
好的,以下是创建存储过程transfer的MySQL代码:
```
DELIMITER //
CREATE PROCEDURE transfer (IN outAccount VARCHAR(20), IN inAccount VARCHAR(20))
BEGIN
DECLARE outBalance DECIMAL(10, 2);
DECLARE inBalance DECIMAL(10, 2);
SELECT balance INTO outBalance FROM accounts WHERE account_number = outAccount;
SELECT balance INTO inBalance FROM accounts WHERE account_number = inAccount;
IF outBalance >= 1000 THEN
UPDATE accounts SET balance = outBalance - 1000 WHERE account_number = outAccount;
UPDATE accounts SET balance = inBalance + 1000 WHERE account_number = inAccount;
SELECT CONCAT('Transfer successful: 1000 yuan transferred from ', outAccount, ' to ', inAccount) AS result;
ELSE
SELECT 'Transfer failed: not enough balance' AS result;
END IF;
END //
DELIMITER ;
```
以上代码中,我们创建了一个名为transfer的存储过程,该过程接收两个参数:outAccount和inAccount,代表转出账号和转入账号。该存储过程会首先查询转出账号和转入账号的余额,如果转出账号余额足够,则进行转账操作,并返回转账成功的信息;否则返回转账失败的信息。
接下来,我们可以通过以下代码调用该存储过程,模拟银行从outAccount账号转账1000元到inAccount账号:
```
CALL transfer('outAccount', 'inAccount');
```
注意,上述代码中的outAccount和inAccount应替换为真实的账号。如果转账成功,将会返回如下信息:
```
+-------------------------------------------------------+
| result |
+-------------------------------------------------------+
| Transfer successful: 1000 yuan transferred from ... |
+-------------------------------------------------------+
```
如果转账失败,则会返回如下信息:
```
+---------------------------+
| result |
+---------------------------+
| Transfer failed: not enough balance |
+---------------------------+
```
希望这个回答对您有所帮助!
阅读全文