I have a cursor for loop
include credit and debit amount of my daily transaction account, and I want to show them respectively.
DECLARE
v_debit := 0;
v_credit := 0;
CURSOR c_acct_rec is
select bk.acctname, bk.bsb, bk.accountnr, bk.value, bk.code
from mybank_tbl bk
r_acct_rec c_acct_rec%ROWTYPE;
FOR r_acct_rec in c_acct_rec LOOP
if r_acct_rec.code = 'dr' then
v_debit := show_debit;
else
v_credit := v_credit + r_acct_rec;
end if;
DBMS_OUTPUT.PUT_LINE(v_credit || v_debit);
END LOOP;
END;
This part of the SQL query from the above is showing all the debit and credit values.
+-----------------+
| value | code | |
+-----------------+
| | 12 | dr | |
| | 5 | dr | |
| | 7 | dr | |
| | 33 | dr | |
| | 16 | dr | |
| | 1000 | cr | |
+-----------------+
The problem I found because they are in the cursor loop, it will populate all value in one column like below
+-------+--------+
| Debit | Credit |
+-------+--------+
| 12 | |
| 5 | |
| 7 | |
| 33 | |
| 16 | |
| 1000 | |
+-------+--------+
The if/else statement can't control it to show all row records.
+-------+--------+
| Debit | Credit |
+-------+--------+
| 12 | 0 |
| 5 | 0 |
| 7 | 0 |
| 33 | 0 |
| 16 | 0 |
| 1000 | 0 |
| 1000 | 1000 |
+-------+--------+
Is that a way to separate it using a stored procedure or separate the row from the account value (r_acct_rec.value) to check to get this expected outcome?
+-------+--------+
| Debit | Credit |
+-------+--------+
| 12 | |
| 5 | |
| 7 | |
| 33 | |
| 16 | |
| | 1000 |
+-------+--------+
==
which isn't a valid operator, you have a strayand
before thethen
, I'm guessing thatdr
is supposed to be the string'dr'
, isshow_debit
really a function that takes no parameters and returns a value? If so, what is that supposed to do. And then you're addingv_acct_rec
, a pseudorecord, tov_credit
which I assume is a local numeric variable which doesn't make sense. – Justin Cave Jun 2 '19 at 2:57v_credit
andv_debit
on each iteration of the loop to NULL if you want one of the two to be NULL and the other to be non-NULL. – Justin Cave Jun 2 '19 at 2:59