如何检查游标是否返回任何记录在oracle? [英] How to check if cursor returns any records in oracle?

查看:158
本文介绍了如何检查游标是否返回任何记录在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;


推荐答案

无法检查游标是否返回记录打开它。

(请参阅 here

所以你可以快速查询是否有记录(例如使用count),

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),

或者,您可以这样做:

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;

这篇关于如何检查游标是否返回任何记录在oracle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆