0

在我看来,我正在编写一个函数,以便调用类似

select get_foo() from dual;

或者

select * from table (get_foo);

返回相同的结果

select * from foo;

所以,我有一个可以编译的函数......

create or replace function get_foo return sys_refcursor as
  rc_foo sys_refcursor;
begin
  open rc_foo for 'select * from foo';
  return rc_foo;
end;

但 select get_foo() from dual 返回 1 行。

((ID=1,NAME=Sarah1),(ID=2,NAME=Sarah2),(ID=3,NAME=Sarah3),)

而 select * from table( get_foo() ) 给了我 ORA-22905。

如何更改函数定义和/或调用以获得所需的结果?

4

1 回答 1

1

您使用流水线功能。

例如:

SQL> create table foo(id , name) as select rownum, 'Sarah'||rownum from dual connect by level <= 3;

Table created.

SQL> create or replace package pipeline_test
  2  as
  3    type foo_tab is table of foo%rowtype;
  4    function get_foo
  5      return foo_tab PIPELINED;
  6  end;
  7  /

Package created.

SQL> create or replace package body pipeline_test
  2  as
  3    function get_foo
  4      return foo_tab PIPELINED
  5    is
  6      v_rc sys_refcursor;
  7             t_foo foo_tab;
  8
  9    begin
 10      open v_rc for select * from foo;
 11      loop
 12        fetch v_rc bulk collect into t_foo limit 100;
 13        exit when t_foo.count = 0;
 14        for idx in 1..t_foo.count
 15        loop
 16          pipe row(t_foo(idx));
 17        end loop;
 18      end loop;
 19    end;
 20  end;
 21  /

Package body created.

SQL> select * from table(pipeline_test.get_foo());

        ID NAME
---------- ---------------------------------------------
         1 Sarah1
         2 Sarah2
         3 Sarah3
于 2013-02-08T00:03:46.473 回答