Stored procedures 从存储过程中的sys\u refcursor获取特定字段?

Stored procedures 从存储过程中的sys\u refcursor获取特定字段?,stored-procedures,oracle9i,sys-refcursor,Stored Procedures,Oracle9i,Sys Refcursor,我正在办公室运行Oracle 9i服务器。我正在处理一个过程,该过程将sys_refcursor作为out参数传递给另一个包(以及其他in参数)。我能够将类型定义为被调用过程在游标中返回的各种列的记录。然后我可以循环使用如下代码: LOOP fetch o_results into v_rec; exit when o_results%notfound; dbms_output.put_line(v_rec.some_id); end loop; 有没有办法只拉一列而不必声明

我正在办公室运行Oracle 9i服务器。我正在处理一个过程,该过程将sys_refcursor作为out参数传递给另一个包(以及其他in参数)。我能够将类型定义为被调用过程在游标中返回的各种列的记录。然后我可以循环使用如下代码:

LOOP
   fetch o_results into v_rec;
   exit when o_results%notfound;
   dbms_output.put_line(v_rec.some_id);
end loop;
有没有办法只拉一列而不必声明整个行类型?我试过这样的方法:

LOOP
  fetch o_results.some_id into v_id;
  exit when o_results%notfound;
  dbms_output.put_line(v_id);
end loop;

但那没用。还有其他想法吗?

没有,如果光标返回包含多个列的结果集,则无法将单个列提取到局部变量(记录除外)中。但是,您确实有一些选择

如果声明强类型游标而不是弱类型游标,则可以基于该游标定义声明局部变量,而不是声明新集合

create or replace procedure cursor_proc
as
  cursor emp_cur
      is
   select empno, ename
     from emp;
  l_row emp_cur%rowtype;
begin
  open emp_cur;
  loop
    fetch emp_cur into l_row;
    exit when emp_cur%notfound;
    dbms_output.put_line( l_row.ename );
  end loop;
  close emp_cur;
end;
或者,如果知道弱类型的ref游标将始终返回特定对象中的所有列,则可以将局部变量声明锚定到该对象。通过声明光标从中选择的视图,始终可以实现这一点。比如说

create or replace view vw_emp
as
select ename, empno
  from emp

create or replace procedure cursor_proc2
as
  emp_cur sys_refcursor;
  l_row   vw_emp%rowtype;
begin
  open emp_cur for select * from vw_emp;
  loop
    fetch emp_cur into l_row;
    exit when emp_cur%notfound;
    dbms_output.put_line( l_row.ename );
  end loop;
  close emp_cur;
end;
最后,如果使用隐式游标,Oracle将隐式声明集合类型

create or replace procedure cursor_proc3
as
begin
  for emp in (select ename, empno from emp)
  loop
    dbms_output.put_line( emp.ename );
  end loop;
end;