如何从plsql函数创建并返回游标?

时间:2011-05-07 20:14:14

标签: plsql oracle11g

我创建了一个plsql函数,我想创建一个游标并从函数中返回此游标。然后我想在Java类中调用此函数并从游标中检索数据。注意:光标返回一行。 我写了这样的东西,

CREATE OR REPLACE
FUNCTION FUNCTION1 ( emp_id IN NUMBER)RETURN cursor AS

  cursor newCursor(e_id number) is  
    select * from table1 where employee_id = e_id;        
    type refCursor is ref cursor;

  BEGIN

  open newCursor(emp_id);    
  loop
  exit when newCursor%notfound;   
    fetch newCursor into refCursor;  
  end loop;
  RETURN refCursor;

END FUNCTION1;

如果我想返回光标,我应该使用什么返回类型?

2 个答案:

答案 0 :(得分:7)

在以下功能之后对其进行建模

create or replace function getemps return sys_refcursor is
v_curs sys_refcursor;
begin
open v_curs for select ename from emp;
return v_curs;
end;
/

答案 1 :(得分:0)

sys_refcursor是oracle的通用隐式游标 改为使用EXPLICITE游标