Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if cursor returns any records in oracle?

I have a following stored procedure in which I have used a cursor. Depending on whether the cursor return any records or not I need to do some processing.

But I am not sure how to check if the cursor return any records.

CREATE OR REPLACE PROCEDURE SP_EMPLOYEE_LOOKUP_BY_EMP_ID
(
      IN_USER_ID IN NUMBER, 
      IN_EMPLOYEE_ID NUMBER,
      IN_HC_AS_ON_DATE VARCHAR2,
      emp_cursor OUT SYS_REFCURSOR
) 
IS 

 CURSOR employees IS 
    SELECT  * FROM EMPLOYEE e; 

BEGIN    

if(record exist ) then 

 FOR employee IN employees
  LOOP  

        // do something  

  END LOOP; 
else if employees is empty then 
     // do something else 

END;
like image 526
ashishjmeshram Avatar asked May 30 '12 10:05

ashishjmeshram


People also ask

How do I find the number of rows returned by a cursor?

Determining the number of rows associated with a cursor can be efficiently done by using the cursor_rowCount scalar function which takes a cursor variable as a parameter and returns an integer value as an output corresponding to the number of rows that have been fetched since the cursor was opened.

What is usually returned by the cursor expressions?

A CURSOR expression returns a nested cursor. This form of expression is equivalent to the PL/SQL REF CURSOR and can be passed as a REF CURSOR argument to a function. A nested cursor is implicitly opened when the cursor expression is evaluated.

What are the 4 cursor attributes?

Every explicit cursor and cursor variable has four attributes: %FOUND , %ISOPEN %NOTFOUND , and %ROWCOUNT . When appended to the cursor or cursor variable, these attributes return useful information about the execution of a data manipulation statement.

How many rows a cursor can hold in Oracle?

there is no limit (i'm aware of) to the number of rows a cursor can process. Obviously, if you try and process 50 million records from a cursor, your program won't likely finish anytime soon.


2 Answers

It's not possible to check if the cursor returns records without opening it.
(see here)
So you can either have some fast query just to see if there are records (using count for example),

Or, you can do it like this:

CREATE OR REPLACE PROCEDURE SP_EMPLOYEE_LOOKUP_BY_EMP_ID
(
      IN_USER_ID IN NUMBER, 
      IN_EMPLOYEE_ID NUMBER,
      IN_HC_AS_ON_DATE VARCHAR2,
      emp_cursor OUT SYS_REFCURSOR
) 
IS 

 is_found_rec boolean := false;    

 CURSOR employees IS 
    SELECT  * FROM EMPLOYEE e; 

BEGIN    

 FOR employee IN employees
  LOOP  

    is_found_rec := true;

        // do something  

  END LOOP; 

 if not is_found_rec then 
     // do something else 
 end if;

END;
like image 152
A.B.Cade Avatar answered Sep 20 '22 18:09

A.B.Cade


I think that it possible only with FETCH. Try to use

if myCursor%found then
// some body
end if;

But if somebody know another way so correct me.

like image 26
Simon Dorociak Avatar answered Sep 19 '22 18:09

Simon Dorociak