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;

ただし、デュアルからget_foo()を選択すると、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 に答える