oracle sql查询的CURSOR中的ROWNUM无法正常工作

时间:2011-11-14 15:05:19

标签: oracle plsql oracle10g

请看下面的oracle函数,

DROP FUNCTION get_mapper_requests;

CREATE OR REPLACE FUNCTION get_mapper_requests(p_status IN NUMBER, p_rownum IN NUMBER)
RETURN  SYS_REFCURSOR
AS

CUR_MED_MAPPER_STATUS SYS_REFCURSOR;

BEGIN

dbms_output.put_line('Status Value:'||p_status);

dbms_output.put_line('Row Count:'||p_rownum);

OPEN CUR_MED_MAPPER_STATUS FOR SELECT cmd_id,status FROM CDFDBA.MED_COM_MAPPER_CMDS WHERE 
STATUS=p_status and ROWNUM < p_rownum;

RETURN CUR_MED_MAPPER_STATUS;

END;

即使我将任何整数值传递给变量p_rownum,上述函数也会返回所有记录。例如,如果p_rownum = 10,则该函数返回所有记录,

调用块

DECLARE

 c SYS_REFCURSOR;

 v  VARCHAR2(1000);

 v1 VARCHAR2(1000);

BEGIN

 dbms_output.put_line('Hello');

 c := get_mapper_requests(0, 10);

 LOOP

    FETCH c INTO v,v1; 

    dbms_output.put_line('Value from cursor: '||v||' '||v1);

 END LOOP;

 CLOSE c;

END;
/

请支持。

2 个答案:

答案 0 :(得分:1)

在调用代码FETCH之后,我认为你错过了

EXIT WHEN c%NOTFOUND;

如果没有这个,我假设您的代码只是继续使用最后一次获取的行调用DBMS_OUTPUT,直到您收到填充dbms输出缓冲区的错误。

答案 1 :(得分:0)

declare
   cursor c is select * from <table_name> where rownum < 15;  -- SELECT ONLY 15 ROWS OF TABLE
   type t is table of c%rowtype index by pls_integer;
   tab t;
begin
   open c;
   loop
      fetch c bulk collect into tab limit 10;   -- FETCH cursor in chunks of 10
      dbms_output.put_line('X'); 
      exit when c%notfound;
   end loop;
end;