如何将表中的值提取到记录表中

时间:2016-06-15 12:20:03

标签: plsql

我需要创建一个返回记录表的函数。将根据函数的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
      );
...

如果类型UROWID生成错误,则添加新的t0列:

  

PLS-00630:流水线函数必须具有受支持的集合返回   型

但我该如何解决呢?

非常感谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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'));
相关问题