簡體   English   中英

如何在結果集中包含單行多列子查詢(PIPELINED函數)結果

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

我正在使用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所有行以及管道函數中的相應值

例如類似

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函數的情況下,如何實現上述預期結果?

試試這個選擇:

    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;

您可以使用OUTER APPLY

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

請使用別名嘗試以下內容

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM