I have make a procedure where two queries return values for
a and b
Substract of a and b is c
c = a-b;
if value of c is positive then procedure return correct value and if value of c is in negative then procedure return NULL.
This is my procedure.
DELIMITER $$
USE `db`$$
DROP PROCEDURE IF EXISTS `getOutStandingBalance`$$
CREATE DEFINER=`root`@`%` PROCEDURE `getOutStandingBalance`(OUT Total DOUBLE SIGNED,IN OID INT)
DETERMINISTIC
COMMENT 'A procedure'
BEGIN
DECLARE Credit DOUBLE;
DECLARE Debit DOUBLE;
SELECT SUM(transaction.Amount) INTO Credit FROM `transaction` WHERE transaction.IsDeleted=0 AND transaction.IsCredit=1 AND transaction.OID=OID;
SELECT SUM(transaction.Amount) INTO Debit FROM `transaction` WHERE transaction.IsDeleted=0 AND transaction.IsCredit=0 AND transaction.OID=OID;
SET Total = (Credit-Debit);
END$$
DELIMITER ;
If no rows are found for either of the credit or debit for the transaction, the variables will be
NULL. In SQL, any calculation with aNULLresults in aNULL.To fix, do this:
Also, change the signature to
or similar. Why are you using
DOUBLE SIGNED?