Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

oracle - local collection types not allowed in SQL statements

I have a question regarding using of collections in Oracle SQL functions.

There are definitions of types in package:

/* Types of package*/
create or replace PACKAGE "test" AS 

TYPE type_record_1 IS record ( id_num NUMBER , timestamp_num NUMBER,value NUMBER);
TYPE type_table_1 IS TABLE OF type_record_1; 
TYPE type_record_2 IS record ( id_num NUMBER , timestamp_num NUMBER,pValue NUMBER);
TYPE type_table_2 IS TABLE OF type_record_2; 
END test;

Problem is in functions_2. function_2 uses output from function_1. The error message occurs when I try select in function_2. Error message "local collection types not allowed in SQL statements".

Could you please help? What is wrong with using of collections in functions?

/*function 1*/
FUNCTION function_1
RETURN  type_table_1
IS
table_1 type_table_1;
BEGIN
-- select values from
SELECT id_num, timestamp_num, value --type_record_1 (id_num, timestamp_num, value) 
BULK COLLECT INTO table_1 
    FROM (
      SELECT       
        l.id_num,
        EXTRACT(hour from end_time) * 60 + EXTRACT(minute from end_time) as timestamp_num,
        l.value
      FROM INTERVAL_F l
      WHERE id_num IN (SELECT id_num FROM table_rev)
    );

 RETURN table_1;

 END function_1;


 /*function 2*/
 FUNCTION function_2
         (
          table_1 IN type_table_1
          )
 RETURN type_table_2
 IS 
 table_2 type_table_2;
 BEGIN

 SELECT type_record_2(id_num , timestamp_num , pValue)
 BULK COLLECT INTO table_2 FROM (
 SELECT  id_num
       , timestamp_num 
       , value as pValue
       FROM table(table_1)  -- ERROR IS HERE
       );

 RETURN table_2;
 END function_2;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To achive that you should use something like:

CREATE OR REPLACE TYPE type_record_1... /

CREATE OR REPLACE TYPE type_table_1 AS TABLE OF type_record_1; /

Oracle does not allow types declared in package to be casted as table. I talk about Oracle until 11, still not check 12c new features :(.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...