简体   繁体   English

函数返回sys_refcursor

[英]Function return sys_refcursor

I have a question regarding sys_refcursor. 我对sys_refcursor有疑问。

A sys_refcursor can only be used with a select statement or can this be used like the following. sys_refcursor只能与select语句一起使用,也可以像下面这样使用。 I would like the function to return sys_refcursor. 我想要该函数返回sys_refcursor。

p_cursor is defined as sys_refcursor p_cursor定义为sys_refcursor

FOR i IN c
    LOOP
        IF (p_start_date BETWEEN i.start_date AND i.end_date)
            OR (p_end_date BETWEEN i.start_date AND i.end_date)
        THEN
            p_cursor := i.product_no;
        END IF;
    END LOOP;

You have two ways of doing this depending on what you want. 您可以根据需要使用两种方法。

First 第一
If you always want to have the column in your cursor, and depending on that condition, to assign a value to the column or not. 如果您始终希望在游标中包含该列,并根据该条件,是否为该列分配一个值。 You can do the following 您可以执行以下操作

Code: 码:

OPEN p_cursor FOR   
SELECT i.columna1, i.columna2,
       (SELECT i.product_no
          FROM dual
         WHERE (p_start_date BETWEEN i.start_date AND i.end_date)
            OR (p_end_date BETWEEN i.start_date AND i.end_date)
        ) as product_no
FROM table_name i

Second 第二
If you want sometimes to have the column and sometimes you just dont. 如果您有时想拥有该专栏,而有时您只是不想。 You can dinamically create the query in a varchar variable and then assign that query to the cursor. 您可以在varchar变量中动态创建查询,然后将该查询分配给游标。

Code: 码:

DECLARE
  var VARCHAR2(200);  
BEGIN

  var := 'select * from dual';

  open p_cursor for var;

END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM