I'm trying to create a stored function in oracle that returns multiple rows.
My question is very similar to this one except that I want to fetch a select *
query
In a nutshell, I want to create a function which returns the result of this query
select * from t_email_queue
What I've tried is this :
create or replace
PACKAGE email_queue AS
type t_email_queue_type is table of T_EMAIL_QUEUE%ROWTYPE;
FUNCTION lock_and_get return t_email_queue_type;
END email_queue;
create or replace
PACKAGE BODY email_queue AS
FUNCTION lock_and_get RETURN t_email_queue_type AS
queue_obj t_email_queue_type;
cursor c (lockid in varchar2) is select * from t_email_queue where lockedby = lockid;
lockid varchar2(100) := 'alf';
BEGIN
OPEN c(lockid);
FETCH c bulk collect INTO queue_obj;
return queue_obj;
END lock_and_get;
END email_queue;
The package compiles just fine but when I try to call it with this query
select * from table(email_queue.lock_and_get);
Oracle throws the following error
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Error at Line: 1 Column: 20
I think Oracle want me to create my return type at the schema level but when I try to do
create type t_email_queue_type is table of T_EMAIL_QUEUE%ROWTYPE;
Oracle complains
Type IMAIL.T_EMAIL_QUEUE_TYPE@imail dev
Error(1): PL/SQL: Compilation unit analysis terminated
Error(2,37): PLS-00329: schema-level type has illegal reference to IMAIL.T_EMAIL_QUEUE
Can someone point me to the right direction? What am I missing here?
Thanks for reading!