2016-11-09 85 views
4

我需要在WITH子句中使用returning_tbl(),然後將WITH子句創建的內聯表作爲參數傳遞給函數。像using_tbl_v2(不此時工作)如何將內聯表作爲參數發送給接收表的函數?

using_tbl_v1只是事物,它們的例子(但他們是簡單的對我來說)。

我意識到,一旦我創建了一個內聯表,我退出PLSQL模式並進入SQL模式。但我怎麼回去PLSQL模式中提供original_tblreceiving_tbl(...)

create or replace type SOME_OBJ force as object (
    SOME_VARCHAR varchar2(20 byte) 
); 

create or replace type SOME_TBL is table of SOME_OBJ; 


create or replace function returning_tbl 
    return SOME_TBL pipelined is 

begin 
    for current_row in (
    select 
     'SOME_VALUE' as SOME_VARCHAR 
    from dual 
) 
    loop 
    pipe row (
     SOME_OBJ(
     current_row.SOME_VARCHAR 
    ) 
    ); 
    end loop; 
    return; 
END returning_tbl; 

select * from table(returning_tbl()); 


create or replace function receiving_tbl(tbl SOME_TBL) 
    return SOME_TBL pipelined is 

begin 
    for current_row in (
    with filtered_tbl as (
     select 
     SOME_VARCHAR 
     from table(tbl) 
     where SOME_VARCHAR = 'SOME_VALUE' 
    ) 
    select * from filtered_tbl 
) 
    loop 
    pipe row (
     SOME_OBJ(
     current_row.SOME_VARCHAR 
    ) 
    ); 
    end loop; 
    return; 
END receiving_tbl; 


select * from table(receiving_tbl(returning_tbl())); 


create or replace function using_tbl_v1 
    return SOME_TBL pipelined is 

begin 
    for current_row in (
    with original_tbl as (
     select 
     SOME_VARCHAR 
     from table(returning_tbl()) 
     where SOME_VARCHAR = 'SOME_VALUE' 
    ), 
    outside_inlined_tbl as (--just as example 
     select * from table(receiving_tbl(returning_tbl())) 
    ) 
    select * from outside_inlined_tbl 
) 
    loop 
    pipe row (
     SOME_OBJ(
     current_row.SOME_VARCHAR 
    ) 
    ); 
    end loop; 
    return; 
END using_tbl_v1; 


select * from table(using_tbl_v1()); 


create or replace function using_tbl_v2 
    return SOME_TBL pipelined is 

begin 

    for current_row in (
    with original_tbl as (
     select 
     SOME_VARCHAR 
     from table(returning_tbl()) 
     where SOME_VARCHAR = 'SOME_VALUE' 
    ), 
    outside_tbl as (
     select * from table(receiving_tbl(original_tbl)) 
    ) 
    select * from outside_tbl 
) 
    loop 
    pipe row (
     SOME_OBJ(
     current_row.SOME_VARCHAR 
    ) 
    ); 
    end loop; 
    return; 
END using_tbl_v2; 


select * from table(using_tbl(_v2)); 

回答

1

替換:

with original_tbl as (
    select 
    SOME_VARCHAR 
    from table(returning_tbl()) 
    where SOME_VARCHAR = 'SOME_VALUE' 
), 
outside_tbl as (
    select * from table(receiving_tbl(original_tbl 
)) 
) 
select * from outside_tbl 

有了:

with original_tbl as (
    select 
    SOME_VARCHAR 
    from table(returning_tbl()) 
    where SOME_VARCHAR = 'SOME_VALUE' 
), 
outside_tbl as (
    select * from table(receiving_tbl(
    (select cast(collect(SOME_OBJ(SOME_VARCHAR)) as SOME_TBL) from original_tbl) 
)) 
) 
select * from outside_tbl 

我我想補充一些關於這裏發生的事情的簡單解釋。但這個例子非常複雜,我不確定是否有任何簡單的教訓可以在這裏學習。

相關問題