2016-01-20 130 views
0
create type data_type_1 as object (x number, y number) 
/

create type table_type_1 as table of data_type_1 
/

create or replace package xyz AS 
     function main_xyz return table_type_1 pipelined; 
     function sub_func return table_type_1 pipelined; 
     function sub_func1 return table_type_1 pipelined; 
end xyz; 
/

create package body XYZ AS 
    function main_xyz return data_type_1 pipelined is 
     begin 
     --code 
     --pipe row(sub_func); --edit_1 
     FOR rec in (select * from table(sub_func1(x,y))) LOOP 
       pipe row(rec); 
     END LOOP; 
     end; 
    --function sub_func return data_type_1 pipelined is --edit_1 
     --begin --edit_1 
     --code --edit_1 
     --pipe row(def); --def is data_type_1 --edit_1 
     --end; --edit_1 
    function sub_func_1(x in number, y in number) return data_type_1 pipelined is 
     begin 
     --code 
     loop 
     pipe row(abc); --abc is data_type_1 
     end loop; 
     end; 
end; 
create package body ABC AS 
    function main_ABC is 
     begin 
     --code 
     FOR rec in (select * from table(main_xyz)) LOOP 
       pipe row(rec); 
     END LOOP; 
     end; 
end; 

錯誤,我得到的...嵌套管道函數

誤差顯示在sub_func1被稱爲main_xyz塊。

[錯誤] PLS-00382():PLS-00382:表達式是錯誤的類型
[錯誤] PLS-00306()的:PLS-00306:錯誤數量或類型的呼叫參數
[錯誤] ORA-00904():PL/SQL:ORA-00904::無效標識符
[錯誤] PLS-00364():PLS-00364:循環索引變量 'REC' 的使用是無效

什麼是上面的代碼錯了?爲什麼?

+0

@diziaq你怎麼看? – user256378

回答

1

你的函數返回data_type_1,表集合也試圖消耗這個。但是兩者都需要一個集合類型,即使您希望它們只返回一個值(在這種情況下,沒有太多的點流水線)。您不能直接管道集合類型,而是管道集合的成員。因此data_type_1應該是標量或對象/記錄類型,並且您需要另一個類型,它們是這些類型的集合。

create type data_type_1 as object (x number, y number) 
/

create type table_type_1 as table of data_type_1 
/

create or replace package xyz AS 
    function main_xyz return table_type_1 pipelined; 
    function sub_func return table_type_1 pipelined; 
    function sub_func1 return table_type_1 pipelined; 
end xyz; 
/

create or replace package body xyz as 
    function main_xyz return table_type_1 pipelined is 
    begin 
    --code 
    for rec in (select * from table(sub_func)) loop 
     pipe row(data_type_1(rec.x, rec.y)); 
    end loop; 
    for rec in (select * from table(sub_func1)) loop 
     pipe row(data_type_1(rec.x, rec.y)); 
    end loop; 
    end; 

    function sub_func return table_type_1 pipelined is 
    def data_type_1; 
    begin 
    --code 
    pipe row(def); --def is data_type_1 
    end sub_func; 

    function sub_func1 return table_type_1 pipelined is 
    abc data_type_1; 
    begin 
    --code 
    loop 
     pipe row (abc); --abc is data_type_1 
    end loop; 
    end sub_func1; 
end xyz; 
/

所以我添加了一個表類型現有data_type_1,並且改變了函數定義爲返回表類型來代替。 pipe row仍然使用data_type_1 - 每個都是表格類型中的一行。你的循環需要查詢它的光標,而不是直接調用table(),所以我也改變了它。並且pipe row(sub_func);也需要在查詢上是類似的循環。

你只標記此爲PL/SQL,但是因爲你可能打算叫main_xyz從普通的SQL,因爲你是從這些循環SQL環境中調用子功能,data_type_1table_type_1需要在被創建模式級別而不是PL/SQL。 (這有changed a bit in 12c,但不足以幫助這裏)。

如果你想有他們作爲PL/SQL類型,在包規範中聲明,那麼你可以不叫從非PL/SQL方面的功能,你就必須更換一個環調用函數,然後對返回的集合進行迭代。

+0

是的我已經實現了你所描述的方式,儘管如此,我得到的錯誤.. – user256378

+0

@ user256378 - 我錯過了循環語法中的一些括號;他們必須是模式級別的類型才能在這些遊標循環中使用它們;我沒有正確引用'rec'字段。至少用更新的東西進行編譯。 –

+0

我再次編輯了我的問題。 – user256378