我在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的输入执行存储过程时,我得到SQL错误,因为列'accountno'不能为空。
当我提到时,出现此错误的原因必须是该列必须声明为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)
这是什么问题?我不能得到它?我的手术是否正确?
答案 0 :(得分:0)
得到了答案。我没有初始化变量。这是更新的程序:
-- --------------------------------------------------------------------------------
-- 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