I have created a stored procedure in MySQL for the debit operation as follows:
-- --------------------------------------------------------------------------------
-- 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 $$
The tables are as follows:
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)
Now, When I execute the stored procedure with the input as ssnno=2532 and amt=100, I get the SQL error as Column 'accountno' cannot be null.
When I refered, the reason of this error must be that the column must be declared as NOT NULL. So I changed that too.
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)
What is the issue here? I can't get it? Is my procedure correct?
Got the answer. I didn't initialize the variables. Here is the updated procedure:
-- --------------------------------------------------------------------------------
-- 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.