I currently am trying to access total owed and total paid for every expense from a financial database. To elaborate, say I have 5 expenses. Each expense has many items in it whose totals can be added up to get its expense's total owed. The expenses also has an amount that has already been paid. I need to display whether or not these numbers(owed and paid) are equal for each expense in the table. I have the following:
SET ECHO OFF
SET VERIFY OFF
SET SERVEROUTPUT ON
DECLARE
paid NUMBER;
owed NUMBER;
ExpNumber NUMBER;
CURSOR CurrentNum IS
SELECT ExpNum FROM ExpMast;
CURSOR walkthrough IS
SELECT SUM(Amt)
FROM ExpDet WHERE ExpNum = ExpNumber;
CURSOR wlkthr IS
SELECT SUM(CashAmt+Amt) FROM
ExpMast NATURAL JOIN ExpByCC WHERE ExpNum = ExpNumber;
BEGIN
OPEN walkthrough;
OPEN wlkthr;
OPEN CurrentNum;
LOOP
FETCH walkthrough INTO owed;
FETCH wlkthr INTO paid;
FETCH CurrentNum INTO ExpNumber;
EXIT WHEN CurrentNum%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Calculating expenses for Exp: ' || ExpNumber);
IF paid = owed THEN
DBMS_OUTPUT.PUT_LINE('All Expenses paid. Total: ' || owed);
ELSE
DBMS_OUTPUT.PUT_LINE('Amount Paid: ' || paid);
DBMS_OUTPUT.PUT_LINE('Total Owed: ' || owed);
DBMS_OUTPUT.PUT_LINE('Difference: ' || (owed-paid));
END IF;
END LOOP;
CLOSE walkthrough;
CLOSE wlkthr;
CLOSE CurrentNum;
END;
/
SET VERIFY ON
SET ECHO ON
However when I run this, it shows the owed and paid for each expense as null. Any ideas as to where I am going wrong with my code?