How to check if cursor returns any records in oracle?

后端 未结 5 1910
旧时难觅i
旧时难觅i 2020-12-11 02:41

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

5条回答
  •  粉色の甜心
    2020-12-11 03:13

    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;
    

提交回复
热议问题