2016-06-15 101 views
0

我需要創建一個函數來返回一個記錄表。過濾將基於函數的IN參數執行。如何從表中提取值到記錄表中

一般情況下,這將是很容易用下面的代碼進行:

CREATE TABLE TABLE_A 
(
    TEST_1 NUMBER 
, TEST_A VARCHAR2(1 BYTE) 
, TEST_B VARCHAR2(1 BYTE) 
) ; 



Insert into TABLE_A (TEST_1,TEST_A,TEST_B) values (1,'a','b'); 
Insert into TABLE_A (TEST_1,TEST_A,TEST_B) values (2,'a','b'); 


create or replace package test_package as 
    type rec is record 
     (t1 table_a.test_1%type 
     , t2 table_a.test_a%type 
     , t3 table_a.test_b%type 
    ); 


    TYPE col_table_1 is table of rec; 

    function test_plsql_table(par1 varchar2) return col_table_1 pipelined; 

end; 


create or replace package body test_package as 

    function test_plsql_table(par1 varchar2) return col_table_1 PIPELINED as 

    cursor temp_cur is 
     SELECT * FROM table_a where test_a = par1; 
    begin 
    for cur_rec in temp_cur loop 
     pipe row(cur_rec); 
    end loop; 
    end; 
end; 

SELECT * from TABLE(test_package.test_plsql_table('a')); 

,但是當我想改變REC記錄的結構到

... 
    type rec is record 
     (t0 UROWID 
     , t1 table_a.test_1%type 
     , t2 table_a.test_a%type 
     , t3 table_a.test_b%type 
    ); 
... 

添加新的T0問題引發列如果類型UROWID生成我的錯誤:

PLS-00630: pipelined functions must have a supported collection return type

但我該如何解決它?

非常感謝。

+0

'表A'需要改變,而新的't0'必須添加 – piyushj

回答

0
PLS-00630:  pipelined functions must have a supported collection return type. 

原因:使用不支持的返回類型指定了流水線函數。被not支持作爲回報類型的流水線功能下列:

- non-collections, 

- PL/SQL tables, 

- associative arrays, 

- collections of PL/SQL types: rowid, mlslabel, long, long raw, boolean, binary_integer, pls_integer, string and urowid 

以下限制:

- If the return type is a collection of records, then each of the attributes of the record must be a supported type. 

    - A collection of records must not contain a record type as one of its attributes. 

行動:指定支持的集合類型的管道函數的返回類型。

文檔: - https://community.oracle.com/thread/2375082?tstart=0

+0

是的,的確,流水線選項不允許返回UROWID dataype。即使不使用此選項,是否有任何方法可以完成我的任務?你能給我一個提示嗎? – mikcutu

0

我希望下面的示意圖可以幫助你克服這些問題。

--Table Creation 
CREATE TABLE TABLE_A 
    (
    TEST_1 NUMBER , 
    TEST_A VARCHAR2(1 BYTE) , 
    TEST_B VARCHAR2(1 BYTE) 
) ; 

--Data Insertion 
INSERT INTO TABLE_A 
    (TEST_1,TEST_A,TEST_B 
) VALUES 
    (1,'a','b' 
); 
INSERT INTO TABLE_A 
    (TEST_1,TEST_A,TEST_B 
) VALUES 
    (2,'a','b' 
); 

    CREATE OR REPLACE TYPE REC IS OBJECT 
    (
    t1 NUMBER, 
    t2 VARCHAR2(1) , 
    t3 VARCHAR2(1) , 
    t0 VARCHAR2(100)); --This can handle the ROWID part too 


CREATE OR REPLACE TYPE REC_TAB IS TABLE OF REC; 

CREATE OR REPLACE PACKAGE test_package 
AS 
    FUNCTION test_plsql_table 
    (
     par1 VARCHAR2 
    ) 
    RETURN REC_TAB; 
END; 

SHOW ERROR; 


CREATE OR REPLACE PACKAGE body test_package 
AS 
FUNCTION test_plsql_table 
    (
    par1 VARCHAR2 
) 
    RETURN REC_TAB 
AS 
lv_tab REC_TAB; 
BEGIN 
    SELECT rec(test_1,test_a,test_b,ROWID) BULK COLLECT INTO lv_tab FROM TABLE_A 
    WHERE test_a = par1; 
    RETURN lv_tab; 
END; 
END; 

--Function execution 
SELECT * FROM TABLE(test_package.test_plsql_table('a')); 
+0

好了,要求是:一列應該是UROWID,應該使用RECORDs表 – mikcutu

+0

所以你想把ROWID放在UROWID數據類型中?是這樣嗎? –

+0

是的,這是正確的,也需要使用記錄表 – mikcutu

相關問題