我正在使用Oracle 12c。在PL/SQL我可以做到这一点PL SQL for循环Sys_RefCursor
set serveroutput on
declare
begin
for x in (select 1 as y from dual) loop
dbms_output.put_line(x.y);
end loop;
end;
我也能做到这一点...
set serveroutput on
declare
cursor c1 is
select 1 as y from dual;
begin
for x in c1 loop
dbms_output.put_line(x.y);
end loop;
end;
到目前为止,一切都很好。但我可以用sys_refcursor来做到这一点吗?我知道我可以取回/ while循环做,但更喜欢for循环的语法(我认为这是有很多清洁)...
set serveroutput on
declare
cur sys_refcursor;
begin
cur := Package.GetData(1234);
fetch cur into y;
while cur%FOUND loop
dbms_output.put_line(y);
fetch cur into y;
end loop;
end;
我想做...
set serveroutput on
declare
cur sys_refcursor;
begin
cur := PACKAGE.GetData(1234); -- This returns a sys_refcursor
for x in cur loop
dbms_output.put_line(x.y);
end loop;
end;
Error report -
ORA-06550: line 5, column 16:
PLS-00221: 'cur' is not a procedure or is undefined
是否有通过sys_refcursor进行循环的机制(而不是通过fetch进入/ while循环)?也许在12c中有一些我不知道的新东西...?
我不这么认为;但是fetch/while循环有什么问题? –
你绝对需要使用ref cursor吗? IME,在PL/SQL中工作时很少需要引用游标 - 主要用途是将游标指针传递给非PL/SQL程序,以便它们可以循环,就好像它们自己打开游标一样。 – Boneist
是因为弱类型引用游标([因为%rowtype不起作用](http://stackoverflow.com/q/11187376/266304))而无法声明记录的问题,无论是因为您不想声明一个记录类型或不知道它将包含什么? 'x.y'参考文献表明你至少知道一些领域? –