# How to display no records found in output on cursor

**URL:** <https://forums.toadworld.com/t/how-to-display-no-records-found-in-output-on-cursor/40586>\
**Category:** Toad for Oracle & DevOps Toolkit\
**Created:** [February 13, 2018, 7:08am UTC](https://forums.toadworld.com/t/how-to-display-no-records-found-in-output-on-cursor/40586 "2018-02-13T07:08:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srivathsava](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/srivathsava/32/313_2.png) [@Srivathsava](https://forums.toadworld.com/u/Srivathsava)\
**Post date:** [February 13, 2018, 7:08am UTC](https://forums.toadworld.com/t/how-to-display-no-records-found-in-output-on-cursor/40586/1 "2018-02-13T07:08:36Z")

</div>

Hi Experts,

I wrote a query as below

SET SERVEROUTPUT ON;  
DECLARE  
CURSOR c\_inv  
IS  
SELECT api.invoice\_num,api.creation\_date,api.invoice\_id  
,api.last\_updated\_by,api.last\_update\_login,api.gl\_date  
FROM AP\_INVOICES\_ALL api  
,AP\_BATCHES\_ALL apb  
WHERE 1=1  
AND apb.batch\_id=api.batch\_id  
AND apb.batch\_name=‘5Feb2018-Btch1’  
AND api.cancelled\_date IS NULL;  
BEGIN  
DBMS\_OUTPUT.PUT\_LINE (‘Start of Block’);  
FOR c\_inv\_r IN c\_inv  
LOOP  
DBMS\_OUTPUT.PUT\_LINE('Invoice# '||c\_inv\_r.invoice\_num);  
END LOOP;  
DBMS\_OUTPUT.PUT\_LINE (‘End of Block’);  
EXCEPTION  
WHEN OTHERS THEN  
DBMS\_OUTPUT.PUT\_LINE ('Error: '||SQLERRM);  
END;

I need to display output as ‘No invoices found for given batch’ if that batch has no invoices. Can any one please help me on how to achieve this using FOR LOOP.

---

<div class="post-metadata">

**Author:** ![Dennis\_Paulus](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/dennis_paulus/32/138_2.png) [@Dennis\_Paulus](https://forums.toadworld.com/u/Dennis_Paulus)\
**Post date:** [February 13, 2018, 8:54am UTC](https://forums.toadworld.com/t/how-to-display-no-records-found-in-output-on-cursor/40586/2 "2018-02-13T08:54:23Z")

</div>

This forum is really for Toad specific questions, but since this is a simple question, here you go…simply add a counter and then check the counter after the loop - if it’s zero, then the cursor query didn’t grab anything.

SET SERVEROUTPUT ON;

DECLARE  
l\_counter number :=0;  
CURSOR c\_inv  
IS  
SELECT api.invoice\_num,api.creation\_date,api.invoice\_id  
,api.last\_updated\_by,api.last\_update\_login,api.gl\_date  
FROM AP\_INVOICES\_ALL api  
,AP\_BATCHES\_ALL apb  
WHERE 1=1  
AND apb.batch\_id=api.batch\_id  
AND apb.batch\_name=‘5Feb2018-Btch1’  
AND api.cancelled\_date IS NULL;

BEGIN  
DBMS\_OUTPUT.PUT\_LINE (‘Start of Block’);  
FOR c\_inv\_r IN c\_inv  
LOOP  
l\_counter := l\_counter + 1;  
DBMS\_OUTPUT.PUT\_LINE('Invoice# '||c\_inv\_r.invoice\_num);  
END LOOP;  
if l\_counter = 0 then  
dbms\_output.put\_line(‘No invoices found for given batch’);  
end if;  
DBMS\_OUTPUT.PUT\_LINE (‘End of Block’);  
EXCEPTION  
WHEN OTHERS THEN  
DBMS\_OUTPUT.PUT\_LINE ('Error: '||SQLERRM);  
END;
