Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

cursor FOR loop to process query if no rows returned

Shambo2904Apr 20 2021

Hi
I am using 12c and have a query if someone could possibly help.
I am using cursors and FOR loops within a database package , a sample of which is below ..
When the first ln_fitment_history_ol, ln_fitment_history_id is passed ie (1, 114211) there's data brought back from the c1 cursor.
However for the next one (1,118436) there's no rows returned thus skipping everything within that FOR loop ie processing of Cursor C2.
I dont want it to skip that process if it finds no data but to assign some default dummy data to be returned for each column in C1 cursor where a.fitment_history_ol can be substituted with the input parameter ln_fitment_history_ol and a.fitment_history_id be substituted with the input parameter ln_fitment_history_id or some other values if easier so that C2 gets processed albeit with dummy data.
How would the c1 cursor select be changed to bring back some data in order for processing to commence within it rather than be skipped over

Regards

ln_fitment_history_ol - 1, ln_fitment_history_id - 114211
ln_fitment_history_ol - 1, ln_fitment_history_id - 118436
ln_fitment_history_ol - 1, ln_fitment_history_id - 118661

CURSOR c1 ( ln_fitment_history_ol NUMBER, ln_fitment_history_id NUMBER) IS
SELECT distinct a.assembly_lifing_rule_ol,
a.assembly_lifing_rule_id,
a.fitment_history_ol,
a.fitment_history_id,
b.li_parameter
FROM life_parameter_usage a,
assembly_lifing_rule b
WHERE a.fitment_history_ol = ln_fitment_history_ol
AND a.fitment_history_id = ln_fitment_history_id
AND a.assembly_lifing_rule_ol = b.lifing_rule_ol
AND a.assembly_lifing_rule_id = b.lifing_rule_id;

FOR i in c1 ( x.fitment_history_ol, x.fitment_history_id ) LOOP -- * c1

OPEN c2 ( I.assembly\_lifing\_rule\_ol, I.assembly\_lifing\_rule\_id, I.fitment\_history\_ol, I.fitment\_history\_id);  
FETCH c2 into ln\_usage;  
CLOSE c2;  

END LOOP; -- c1

This post has been answered by James Su on Apr 20 2021
Jump to Answer
Comments
Post Details
Added on Apr 20 2021
7 comments
2,091 views