首页 > 数据库 > Oracle > Oracle动态语句中返回游标
本来以为动态语句只能返回类似int、varchar2这种类型,今天测试了下,发现还支持游标,现测试如下:
创建返回游标的函数:
create or replace function testf return sys_refcursor as
v_refcur sys_refcursor;
begin
open v_refcur for
select * from test;
return v_refcur;
end;
测试动态语句返回游标到变量的情况:
declare
type tbl_numlist is table of pls_integer;
v_numlist tbl_numlist;
v_refcur sys_refcursor;
begin
--方法1:
execute immediate 'select testf() from dual' into v_refcur;
fetch v_refcur bulk collect into v_numlist;
for i in 1 .. v_numlist.count loop
dbms_output.put_line(v_numlist(i));
end loop;
close v_refcur;
--方法2:
execute immediate 'begin :1 := testf(); end;' using out v_refcur;
fetch v_refcur bulk collect into v_numlist;
for i in 1 .. v_numlist.count loop
dbms_output.put_line(v_numlist(i));
end loop;
close v_refcur;
end;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12932950/viewspace-2120701/ ,如需转载,请注明出处,否则将追究法律责任。