I am inserting into TABLE_A as given below.
INSERT
INTO Table_A (house_id,
house_key_nbr,
mnty_code,
split)
SELECT wcp.id,
ld.ld_ln_id,
ld.ld_mnty,
ROUND((ld.ld_ln_bal/wla.LOAN_AMT) * 100,2) split
FROM table_B ld,
table_C cc,
TABLE_D wcp,
TABLE_E wla
WHERE cc.conv_id = I_conv_id
AND cc.ev_id = wcp.ev_id
AND cc.client_plan_nbr = ld.plan_id
AND wcp.ssn = ld.ssn
AND wla.house_id = wcp.id
AND wla.house_key_nbr = ld.ld_ln_id
AND ld.status_code in ('V','W');
Once i have loaded into the table_A then i created a cursor to find out the records having the sum of split not equal to 100. For those cases I will find the diff and then update the record as given below.
CURSOR max_percent IS
SELECT house_id,
house_key_nbr,
sum(split) percent_sum
FROM TABLE_A s1,
TABLE_D p1,
table_C c1
WHERE s1.house_id = p1.id
AND p1.ev_id = c1.ev_id
AND c1.conv_id = I_conv_id
GROUP BY house_id, house_key_nbr
HAVING SUM(split) != 100;
OPEN max_percent;
l_debug_msg:='Cursor Opened';
FETCH max_percent BULK COLLECT INTO mnty_rec;
l_debug_msg:='Fetching the values from cursor';
FOR i IN 1..mnty_rec.COUNT
LOOP
v_diff := 100.00 - mnty_rec(i).percent_sum;
l_debug_msg:='The difference is '||v_diff||' for the house_id : '||mnty_rec(i).house_id;
UPDATE work_conv_part_loan_mnty_splt wcplms
SET split = split + v_diff
WHERE wcplms.house_id = mnty_rec(i).house_id
AND wcplms.house_key_nbr = mnty_rec(i).house_key_nbr
AND rownum = 1;
l_debug_msg:='Updated the percentage value for the house_id'||mnty_rec(i).house_id ;
END LOOP;
CLOSE max_percent;
The question here is, I achieved this simple process using a cursor. Is there any way I can achieve it during the insertion time itself instead of writing the cursor?