Creating a function to return a table of records
85558Dec 29 2010 — edited Dec 30 2010Okay, I thought I knew how to do this but apparently not. I have a very complex query that connects multiple tables. I want to put the results of that query into a "table" (non-persistent) that can be passed to another procedure. So, I created an object/record that defines a single row in this table:
create or replace TYPE SHP_RECORD is OBJECT
(FIELD01 NUMBER(10),
FIELD02 NUMBER(10),
FIELD03 NUMBER(10),
FIELD04 NUMBER(10),
FIELD05 NUMBER(10),
FIELD06 VARCHAR2(200),
FILED07 NUMBER(10),
FIELD08 VARCHAR2(200),
FIELD09 NUMBER(10),
FIELD10 TIMESTAMP(6),
FIELD11 TIMESTAMP(6),
FIELD12 TIMESTAMP(6),
FIELD13 VARCHAR2(5),
FIELD14 NUMBER(10),
FIELD15 VARCHAR2(100),
FIELD16 VARCHAR2(4000),
FIELD17 VARCHAR2(1),
FIELD18 VARCHAR2(1));
/
Then I create another type that defines a table of SHP_RECORD:
Create or replace TYPE SHP_TABLE is TABLE of SHP_RECORD;
/
Now I have a function that puts the huge query into a text string (because it's got elements that change depending on what day of the week it's being run on so I have to keep it as a dynamic query. Now I want to run this query and put the results into a table. I've changed all the names to protect the innocent in this code snippet:
create or replace function get_SHP_data(p_cust_id IN NUMBER,
p_date IN TIMESTAMP) return SHP_TABLE as
begin
declare
shp_data_out SHP_TABLE;
TYPE shp_cur_type is REF CURSOR;
shp_cv shp_cur_type;
TYPE daily_query is VARRAY(7) of VARCHAR2(15);
query_values DAILY_QUERY;
day_index NUMBER;
old_program_id NUMBER;
chk_rundown NUMBER;
query_text VARCHAR2(3000);
row_index NUMBER;
program_freq_id NUMBER;
prog_seg_count NUMBER;
upload_seg_count NUMBER;
xfer_count NUMBER;
sched_count NUMBER;
bill_count NUMBER;
rcvr_text VARCHAR2(2000);
xmsn_start TIMESTAMP;
xmsn_end TIMESTAMP;
epi_status VARCHAR2(2000);
begin
query_values := daily_query('1, 3, 4, 12','1, 2, 5, 12','1, 2, 6, 12','1, 2, 7, 12','1, 2, 8, 12','1, 2, 9, 12','1, 3, 10, 12');
day_index := to_number(to_char(p_date,'D'));
query_text := {really ugly query here that includes concatinating a value from query_values as well as defines five bind variables numbered :1 through :5};
old_program_id := 0;
open shp_cv for query_text using p_date, p_date, p_date, p_date, p_cust_id;
fetch shp_cv bulk collect into shp_data_out;
close shp_cv;
. . .
end;
end;
Okay, the function compiles just fine. But when I try to run it I get:
select * from table(get_shp_data(226, SYSTIMESTAMP))
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got -
ORA-06512: at "SCHEMA.GET_SHP_DATA", line 69
The line it's blowing up on is "fetch ship_cv bulk collect into shp_data_out" I've checked and verified that the record/object structure SHP_RECORD matches in both type and order the values that are returned by the query. So...what gives? I've been beating my head against this particular problem for several days now and am no closer to a solution.
Any and all suggestions or corrections gratefully appreciated.
Oh, and this is being run in a 10g release 2 environment:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP, Data Mining
and Real Application Testing options
HELP??!!