简体   繁体   English

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

[英]How to include single row multiple column subquery (PIPELINED function) results in the result set

I am using Oracle 11g. 我正在使用Oracle 11g。

So, lets say that I have a test data table like this one 所以,可以说我有一个像这样的测试数据表

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
)

Also I have a piplined function that returns one row per call, with multiple columns like the following: 我还有一个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;
/

And of course the 当然

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

returns 退货

Smih    139289289   Paris

Now I would like a select statement that will return all the rows of the test_data along with the corresponding values from pipeline function 现在我想要一条select语句,该语句将返回test_data所有行以及管道函数中的相应值

eg something like 例如类似

select test_data.*, (select * from table(MY_PACK.runPipe(id))) from test_data

which of course doses not work but the expected results would be something like: 当然哪个不起作用,但是预期结果将是:

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

So how to achieve the above expected result, given the test_data table and the pipelined function? 那么,在给定test_data表和pipelined函数的情况下,如何实现上述预期结果?

try this select : 试试这个选择:

    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;

You could use OUTER APPLY : 您可以使用OUTER APPLY

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

Kindly Try the below with alias 请使用别名尝试以下内容

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何订购在单个结果集中包含多个组的正确方法? - How to order the right way to include multiple groups in a single results set? 如何从子查询中引用函数结果列? - How to reference column of results of function from subquery? SQL:如何在WHERE子句中包含SubQuery列结果? - SQL: How to include SubQuery column result in WHERE clause? 如何使用单列返回部分搜索结果,然后让结果可单击以显示该结果的整行? - How to have partial search return results with a single column then have the results clickable to show entire row of that result? 如何返回某一列是子查询结果的行,并返回另一列中最高为0或同一子查询结果的列? - How to return a row where a column is result of subquery and return another column which is highest of 0 or result of same subquery? 用子查询结果更新多行 - Update multiple row with result of subquery SQL:子查询单行或多行 - SQL : subquery single or multiple row 将列值设置为子查询结果 - Set a column value to a subquery result 使用`newid()`将单行子查询与列交叉连接会导致每行具有不同的GUID - Cross joining a single row subquery with a column using `newid()` results in every row having a different GUID 将索引添加到临时表中可得到单行结果集 - Adding index to temporary table results in single row result set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM