Home » SQL & PL/SQL » SQL & PL/SQL » checking if cursor is returning empty list (oracle 10g)
checking if cursor is returning empty list [message #476585] Fri, 24 September 2010 01:42 Go to next message
saaya123
Messages: 76
Registered: September 2010
Member
I am select list of ids;

cursor c1 is
select c_id from ctab1 where login = l_id
union
select c_id from ctab2 where login = i_id;

now I need to return err_code and err_message if this combined list is empty !
Can not use exit%notfound. help please !!

TIA

Re: checking if cursor is returning empty list [message #476587 is a reply to message #476585] Fri, 24 September 2010 01:45 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Fetch and use NO_DATA_FOUND exception.

PL/SQL User's Guide and Reference
Application Developer's Guide - Fundamentals

Regards
Michel

[Updated on: Fri, 24 September 2010 01:45]

Report message to a moderator

Re: checking if cursor is returning empty list [message #476591 is a reply to message #476587] Fri, 24 September 2010 02:08 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Cursor + NO_DATA_FOUND? Is it not C1%NOTFOUND?
Re: checking if cursor is returning empty list [message #476597 is a reply to message #476591] Fri, 24 September 2010 03:00 Go to previous messageGo to next message
nestor
Messages: 34
Registered: September 2010
Member

This link might be of big help to you.
http://www.java2s.com/Tutorial/Oracle/0500__Cursor/Raisenodatafoundexceptionifcursorisempty.htm
Re: checking if cursor is returning empty list [message #476599 is a reply to message #476597] Fri, 24 September 2010 03:04 Go to previous messageGo to next message
saaya123
Messages: 76
Registered: September 2010
Member


thank you all Smile
Re: checking if cursor is returning empty list [message #476602 is a reply to message #476599] Fri, 24 September 2010 03:12 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
You might want to rethink this logic. Exception handling should only be there in case the normal logic does not suffise. IF you say "return", that would suggest that you are making a function. Now let me explain with a simple example:

col orafaq1 format a20
col orafaq2 format a20

Create or replace function orafaq1
   Return Varchar2
Is
   -- cursor that won't return a record
   cursor c1
   is
   select 'A' return_value
   From   dual
   Where 1=2;
   
   lv_return Varchar2(20);
Begin
   Open  c1;

   Fetch c1 Into lv_return;

   If c1%NOTFOUND Then
      Close c1;
      Raise No_Data_Found;
   Else
      Close c1;
   End If;

   Return lv_Return;
Exception
   When No_Data_Found Then
      Return 'Nothing found!';
End;
/

Create or replace function orafaq2
   Return Varchar2
Is
   -- cursor that won't return a record
   cursor c1
   is
   select 'A' return_value
   From   dual
   Where 1=2;
   
   lv_return Varchar2(20);
Begin
   Open  c1;

   Fetch c1 Into lv_return;

   If c1%NOTFOUND Then
      lv_Return := 'Nothing found!';
   End If;
   Close c1;
   
   Return lv_Return;
End;
/

Select  orafaq1
     ,  orafaq2
From    dual
/
     
drop function orafaq1
/
drop function orafaq2
/
If you look a the script above, you will notice that both functions do the same but the "orafaq2" function won't use exception handling as part of the logic. Using exceptions this way is bad practice, and might have an influence on performance.

I know that it is a rather silly example but I just want to offer an alternative to using exception handling as part of the regular logic.

MHE
Previous Topic: Query Issue
Next Topic: How to find time offset for given time?
Goto Forum:
  


Current Time: Wed May 06 02:02:29 CDT 2026