How to make advanced operations on SYS_REFCURSOR?
13:04 04 Feb 2021

I have several procedures and functions that should make some computations on the same set of data (such that if I change the query of this set of data, the change will be reflected on all the other functions).

What I'm trying to do is creating a function that generates a SYS_REFCURSOR corresponding to the shared set of data.

For example:

MyTable : table(ID number, Amount number);
Type RefCursor** IS REF CURSOR RETURN MyTable%ROWTYPE;

The function that returns a cursor for the shared set of data :

function GetSharedSetOfData(SomeArgument varchar2) RETURN RefCursor is                
     Cur  RefCursor;
begin
    Open Cur for select * from MyTable where 
                        Condition1 and/or Condition2;
    return Cur;
end;

This function should be used in other functions like :

-- Function A
function Function_A_GetTheTotalOfAllRows return number is 
    Output number;
begin
    select sum(Amount) into Output from table(CAST(GetSharedSetOfData(SomeArgumentValue) as MyTable%rowtype));
    return Output;
end;

-- Function B
function Function_B_GetPartialTotalOfAllRows return number is 
    Output number;
begin
    select sum(Amount) into Output from table(CAST(GetSharedSetOfData(SomeArgumentValue) as MyTable%rowtype)) where ID < 100 ;
    return Output;
end;

-- Function C
function Function_C_GetOtherComputation return number is 
    Output number;
begin
    select sum(Amount) into Output from table(CAST(GetSharedSetOfData(SomeArgumentValue) as MyTable%rowtype)) where condition_Xx... ;
    return Output;
end;

The function GetSharedSetOfData compiles and work properly. However the following queries don't compile.

select sum(Amount) into Output from table(CAST(GetSharedSetOfData(SomeArgumentValue) as MyTable%rowtype)); 

I'm getting the error:

ORA-00911: invalid character it refers to the character '%' on MyTable%rowtype

How can I resolve this?

oracle-database