PL/SQL procedure/function to show data from different tables dynamically alongwith the column names in first data row

╄→尐↘猪︶ㄣ 提交于 2021-02-20 02:51:14

问题


I have a requirement like below.

There are 70 tables and I have to build 70 queries from those 70 different tables depending on some condition.

Let us say the table names are TAB_1,TAB_2,TAB_3.....,TAB_70.The number of columns and data type of the columns are different in each table. I will get user input and I have to pass that value to an Oracle PL/SQL function or procedure GET_RESULT() and get the output in tabular format(same as we get when we run a query).

Also, I have to show the column names in the 1st data row.

Example:

I am taking two tables, TAB_1 and TAB_2.

TAB_1

ID Quarter Risk
00001 Q0 2
00001 Q1 3
00001 Q2 1
00001 Q3 1
00001 Q4 2

TAB_2

ID Status
00001 ACTIVE
00002 PURGED
00003 ACTIVE
00004 ACTIVE

If I get user input 1, I will pass it to a procedure's parameter, GET_RESULTS(1) and get an output like below:

Col1 Col2 Col3
ID Quarter Risk
00001 Q0 2
00001 Q1 3
00001 Q2 1
00001 Q3 1
00001 Q4 2

If GET_RESULTS(2) then :

Col1 Col2
ID STATUS
00001 ACTIVE
00002 PURGED
00003 ACTIVE
00004 ACTIVE

Can someone help?


回答1:


Use the data dictionary to build a SQL statement that selects the correct columns. Use dynamic SQL to open a refcursor to that statement, and return the cursor from the function.

Sample Schema

create table tab_1 as
select '00001' id, 'Q0' quarter, 2 risk from dual union all
select '00001' id, 'Q1' quarter, 3 risk from dual union all
select '00001' id, 'Q2' quarter, 1 risk from dual union all
select '00001' id, 'Q3' quarter, 1 risk from dual union all
select '00001' id, 'Q4' quarter, 2 risk from dual;

create table tab_2 as
select '00001' id, 'ACTIVE' status from dual union all
select '00002' id, 'PURGED' status from dual union all
select '00003' id, 'ACTIVE' status from dual union all
select '00004' id, 'ACTIVE' status from dual;

Function

create or replace function get_results(p_id number) return sys_refcursor is
    v_sql varchar2(32767);
    v_refcursor sys_refcursor;
begin
    --Get SQL statement.
    select
        'select ' || 
        listagg(column_name, ',') within group (order by column_id) ||
        ' from ' || table_name
    into v_sql
    from user_tab_columns
    where table_name = 'TAB_' || p_id
        and column_id <= 2
    group by table_name;

    open v_refcursor for v_sql;

    return v_refcursor;
end;
/

Calling the Function

The function should work as long as the application understands refcursors. Below is an example in a recent version of SQL*Plus:

SQL> select get_results(1) from dual;

GET_RESULTS(1)
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

ID    QU
----- --
00001 Q0
00001 Q1
00001 Q2
00001 Q3
00001 Q4


来源:https://stackoverflow.com/questions/62075896/pl-sql-procedure-function-to-show-data-from-different-tables-dynamically-alongwi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!