I
I
iscateli2022-03-05 18:57:41
Oracle
iscateli, 2022-03-05 18:57:41

How to return query results from a function if SQL is generated dynamically?

In a function, a query is dynamically formed, how can I return its results from the function? The problem is that the number of columns in a query can be different each time, and so you can't declare a struct type to return results. Here is a related question, but the structure is pre-defined there.
I found a solution on the Internet, but it is only for a predefined type-structure

create or replace package pack as
    cursor cur (id int) is
        select * 
        from tab where id=cur.id;
    type resty is table of tab%rowtype;  
    function TabById (id int) return resty pipelined;
end;
/
create or replace package body pack as
    function TabById (id int) return resty pipelined is
        r tab%rowtype;
    begin
        open cur (id);
        loop fetch cur into r;
            exit when cur%notfound;
            r.name := '*'||r.name;
            pipe row (r);
        end loop;
        return;
    end;
end;
/

select * from pack.TabById (1);

        ID NAME                                         
---------- ---------------------------------------------
         1 *name 1

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question