'Oracle: Execute SQL query stored in table for further usage in tableau

I have a table ReportingTable having following rows

select * from REPORTINGTABLE;

q_id    q_value
-----------------
q1  select * from customer
q2  select * from pilot
q3  select * from staff

What I have to do is create a query which queries this table and execute the query inside the table in order to get result on basis of q_id, then I need this data to be used in tableau further on what I have tried is creating procedure and function but unluckily haven't got the desired results

Function:

create or replace FUNCTION exec_dynamic_query (
p_q_id varchar
)
return sys_refcursor
is
  stmt REPORTINGTABLE.Q_VAL%TYPE;
  return_val sys_refcursor;
  v_list customer%ROWTYPE;
  begin
    select q_val into stmt from reportingtable where q_id = p_q_id;
    dbms_output.put_line(stmt);
    open return_val for stmt;
    return return_val;
  end;

this function is returning the correct sys_refcursor but I am unable to use it as the result is something like

select exec_dynamic_query('q1') from dual;

Output:

{<CUSTOMERID=C0001,NAME=Jonie Jones,ADDRESS=50 Lesson Lane, Erebor, New Endor,COUNTRY=New Endor,[email protected],PHONE=+4958364765,BIRTHDATE=12-MAY-56,PASSPORTNUM=92736401>,<CUSTOMERID=C0002,NAME=Sarah Delaney,ADDRESS=22 Time Drive, Arcadia, Gallifrey,COUNTRY=Gallifrey,[email protected],PHONE=+9283746198,BIRTHDATE=14-FEB-92,PASSPORTNUM=83748917>,<CUSTOMERID=C0003,NAME=Mark Foster,ADDRESS=64 Bond Street, London, UK,COUNTRY=UK,[email protected],PHONE=+3748264763,BIRTHDATE=30-DEC-89,PASSPORTNUM=32764920>,<CUSTOMERID=C0004,NAME=Patrick Jolie,ADDRESS=18 Park Road, Melbourne,COUNTRY=AUS,[email protected],PHONE=+6334495820,BIRTHDATE=05-MAY-46,PASSPORTNUM=35270848>,<CUSTOMERID=C0005,NAME=Nik Malema,ADDRESS=124 Tenth Street, Cape Town, South Africa,COUNTRY=South Africa,[email protected],PHONE=+5783749100,BIRTHDATE=17-JUN-88,PASSPORTNUM=98562538>}

but what I need is

C0001   Jonie Jones 50 Lesson Lane, Erebor, New Endor   New Endor   [email protected]  +4958364765 12-MAY-56   92736401
C0002   Sarah Delaney   22 Time Drive, Arcadia, Gallifrey   Gallifrey   [email protected] +9283746198 14-FEB-92   83748917
C0003   Mark Foster 64 Bond Street, London, UK  UK  [email protected]    +3748264763 30-DEC-89   32764920
C0004   Patrick Jolie   18 Park Road, Melbourne AUS [email protected]    +6334495820 05-MAY-46   35270848
C0005   Nik Malema  124 Tenth Street, Cape Town, South Africa   South Africa    [email protected]   +5783749100 17-JUN-88   98562538

the issue with the procedure is that I cannot be called in tableau, can you please help me out in that

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source