如何在结果集中包含单行多列子查询(PIPELINED函数)

时间:2017-11-15 08:19:40

标签: sql oracle plsql pipelined-function

我正在使用Oracle 11g。

所以,假设我有一个像这样的测试数据表

with test_data as (
    select 1 as id, 'John' as name from dual 
        union all
    select 2 as id, 'Jack' as name from dual
        union all
    select 3 as id, 'Peter' as name from dual
)

此外,我还有piplined功能,每次通话都会返回一行多列如下所示:

CREATE OR REPLACE PACKAGE MY_PACK AS
  TYPE result_record is RECORD(
   surname           varchar2(27),
   telephone          varchar2(50),
   place_of_birth     varchar2(50)
       );

  TYPE result_table IS TABLE OF result_record;
  function runPipe(id number) RETURN result_table PIPELINED;
END ERC_PACK;
/

CREATE OR REPLACE PACKAGE BODY MY_PACK AS
    function runPipe(id number) RETURN result_table PIPELINED IS
    rec           result_record:=null;
    begin
       if  (id=1) then
         select 'Smih','139289289','Paris' into rec from dual;
       end if;
       if  (id=2) then
         select 'Lock','1888888888','London' into rec from dual;
       end if;
       if  (id=3) then
         select 'May','99999999999','Berlin' into rec from dual;
       end if;
       pipe row (rec);
       return;
  end;
END ERC_PACK;
/

当然还有

select * from table(MY_PACK.runPipe(1)) 

返回

Smih    139289289   Paris

现在我想要一个select语句,它将返回test_data的所有行以及来自管道函数的相应值

e.g。

之类的东西
select test_data.*, (select * from table(MY_PACK.runPipe(id))) from test_data

当然剂量不起作用预期结果类似于:

1 John  Smih    139289289   Paris
2 Jack  Lock    1888888888  London
3 Peter May     99999999999 Berlin

那么在给定test_data表和pipelined函数的情况下如何实现上述预期结果?

3 个答案:

答案 0 :(得分:2)

尝试此选择:

    with test_data as (
    select 1 as id, 'John' as name from dual 
        union all
    select 2 as id, 'Jack' as name from dual
        union all
    select 3 as id, 'Peter' as name from dual
)
select td.*, s.*
from test_data td
LEFT JOIN  table(MY_PACK.runPipe(td.id)) s ON 1 = 1;

答案 1 :(得分:0)

您可以使用OUTER APPLY

select td.*, s.*
from test_data td
outer apply (select * from table(MY_PACK.runPipe(td.id))) s

答案 2 :(得分:0)

请使用别名

尝试以下内容
with test_data as (
select 1 as id, 'John' as name from dual 
    union all
select 2 as id, 'Jack' as name from dual
    union all
select 3 as id, 'Peter' as name from dual
)
select * from table(MY_PACK.runPipe(test_data.id))) from test_data