[英]MySQL Stored Procedure Execution Error
我已经在MySQL中为借记操作创建了一个存储过程,如下所示:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `debit`(IN snnno INT, IN amt INT, OUT bal INT, OUT msg VARCHAR(50))
BEGIN
DECLARE cid INT;
DECLARE actno INT;
DECLARE existbal INT;
SELECT customerid INTO cid FROM customer WHERE ssn=@ssnno;
SELECT accountno INTO actno FROM customeraccounts WHERE customerid=@cid;
SELECT balance INTO existbal FROM account WHERE accountno=@actno;
IF @exisbal < @amt THEN
SET msg = 'You dont have suficient balance';
ELSE
UPDATE account SET account.balance = account.balance - amt WHERE account.accountno = @actno;
INSERT INTO transactions VALUES(NULL, @actno, 'debit', amt);
SELECT balance INTO bal FROM account WHERE account.accountno = @actno;
END IF;
END $$
下表如下:
mysql> select * from customeraccounts;
+------------+-----------+-------------------+
| customerid | accountno | accounttype |
+------------+-----------+-------------------+
| 1 | 101 | Savings |
| 2 | 102 | Savings |
| 3 | 103 | Current |
| 4 | 104 | Fixed Deposit |
| 5 | 105 | Recurring Deposit |
+------------+-----------+-------------------+
5 rows in set (0.00 sec)
mysql> select * from customer;
+------------+--------------+------------------+--------------+------+
| customerid | customername | customerstreet | customercity | ssn |
+------------+--------------+------------------+--------------+------+
| 1 | Amy | Ping Ting Road | Hong Kong | 2532 |
| 2 | Bob | Texaco Road | Hong Kong | 8073 |
| 3 | Carson | Deerfoot Trail | Calgary | 6667 |
| 4 | David | University Drive | Calgary | 4724 |
| 5 | John | Ogden Road | Calgary | 3880 |
+------------+--------------+------------------+--------------+------+
5 rows in set (0.00 sec)
mysql> select * from account;
+-----------+---------+-------------+
| accountno | balance | credit_card |
+-----------+---------+-------------+
| 101 | 700 | 2000 |
| 102 | 2500 | 1000 |
| 103 | 2700 | 1200 |
| 104 | 4000 | 2500 |
| 105 | 4500 | NULL |
+-----------+---------+-------------+
5 rows in set (0.00 sec)
现在,当我使用ssnno = 2532和amt = 100的输入执行存储过程时,由于列'accountno'不能为空,我得到了SQL错误。
当我引用时,此错误的原因必须是该列必须声明为NOT NULL。 所以我也改变了。
mysql> desc account;
+-------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| accountno | int(11) | NO | PRI | NULL | |
| balance | int(11) | NO | | NULL | |
| credit_card | int(11) | YES | | NULL | |
+-------------+---------+------+-----+---------+-------+
3 rows in set (0.01 sec)
这是什么问题? 我听不懂吗 我的程序正确吗?
得到了答案。 我没有初始化变量。 这是更新的过程:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `debit`(IN ssnno INT, IN amt INT, OUT bal INT, OUT msg VARCHAR(45))
BEGIN
DECLARE actno INT DEFAULT 0;
DECLARE cid INT DEFAULT 0;
DECLARE currentbal INT DEFAULT 0;
SELECT customerid INTO cid FROM customer WHERE ssn = ssnno;
SELECT accountno INTO actno FROM customeraccounts WHERE customerid = cid;
SELECT balance INTO currentbal FROM account WHERE accountno = actno;
IF currentbal > amt THEN
UPDATE account SET account.balance = account.balance - amt WHERE account.accountno = actno;
INSERT INTO transactions VALUES(NULL, actno, 'debit', amt);
SELECT balance INTO bal FROM account WHERE account.accountno = actno;
SET msg = 'Successfully Debited';
ELSE
SET msg = 'No Sufficient Balance';
SET bal = currentbal;
END IF;
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.