Home > Back-end >  How to read the same column from every table in a database?
How to read the same column from every table in a database?

Time:10-24

I have a huge database with 400 tables. Each table has the same column id for the Primary key and "timestamp_modify" in which the last change of the table is done.

So what I want are 2 things:

Now I want a list of all changes by ID and table name like:

Table  | id   | timestamp_modiy
Kid    | 1    | 24.10.2021 00:01
Parent | 1000 | 24.10.2021 00:02

The only, very bad way I could come up with, is that I make a view in which I include every damn table by hand and read out the values...

Is there a better way?

CodePudding user response:

Yes, the only way is to union all all tables, like:

select id, timestamp_modify
from kid
union all
select id, timestamp_modify
from parent
union all
...

The performance will be awful, since all the tables will be scanned every time :(

I think that you might reconsider you db design...

CodePudding user response:

You can build a procedure for this, but even so it will have some impact in performance. Although there is a loop, with SQL Dynamic, you might only need 400 iterations, and in each one you will insert all the ids of that table.

I am taking some assumptions

  • You want all the IDs and their corresponding timestamp_modify per table
  • I create a table to store the results. If you use it with the same name always it will recycle the object. If you not, you can keep a history
  • I am assuming that only one timestamp_modify row is present per ID
  • I filter only the tables of your schema that contain both columns.
  • The table contains also the table_name that you can identify where the record is coming from.

One example

create or replace procedure pr_build_output ( p_tmp_table in varchar2 default 'TMP_RESULT' ) 
is
vcounter   pls_integer;
vsql       clob; 
vtimestamp date; -- or timestamp 
begin 
-- create table to store results 
 select count(*) into vcounter from all_tables where table_name = upper(p_tmp_table) and owner = 'MY_SCHEMA';
 if vcounter = 1 
 then 
    execute immediate ' drop table '||p_tmp_table||' purge ' ;
 end if;
 vsql := ' create table '||p_tmp_table||' 
              ( table_name varchar2(128) , 
                id number, 
                timestamp_modify date -- or timestamp
               ) ';
 execute immediate vsql ;

-- Populate rows

for h in 
( select a.table_name from all_tables a 
  where a.owner = 'MY_SCHEMA'
  and a.table_name in ( select distinct b.table_name from all_tab_columns b where b.owner = 'MY_SCHEMA'
                        and b.column_name = 'ID' and b.column_name = 'TIMESTAMP_MODIFY'  
                      )
) 
 loop 
    vsql := ' insert into '||p_tmp_table||' ( table_name , id, timestamp_modify ) 
              select '''||h.table_name||''' as table_name , id , timestamp_modify 
              from my_schema.'||h.table_name||' 
            ' ;
    execute immediate vsql ;
    commit ;
 end loop;
 exception when others then raise;
end;
/

CodePudding user response:

How about a pipelined function?

Just setting datetime format (you don't have to do that):

SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';

Session altered.

Types:

SQL> create or replace type t_row as object
  2    (table_name       varchar2(30),
  3     id               number,
  4     timestamp_modify date)
  5  /

Type created.

SQL> create or replace type t_tab is table of t_row;
  2  /

Type created.

Function: querying user_tab_columns, its cursor FOR loop fetches tables that contain both ID and TIMESTAMP_MODIFY columns, dynamically creates select statement to return the last (MAX function, to avoid too_many_rows) columns' values for the last TIMESTAMP_MODIFY value (returned by the subquery).

SQL> create or replace function f_test
  2    return t_tab pipelined
  3  as
  4    l_str              varchar2(500);
  5    l_id               number;
  6    l_timestamp_modify date;
  7  begin
  8    for cur_r in (select table_name from user_tab_columns
  9                  where column_name = 'ID'
 10                  intersect
 11                  select table_name from user_tab_columns
 12                  where column_name = 'TIMESTAMP_MODIFY'
 13                 )
 14    loop
 15      l_str := 'select max(a.id) id, max(a.timestamp_modify) timestamp_modify ' ||
 16               'from ' || cur_r.table_name   || ' a ' ||
 17               'where a.timestamp_modify = ' ||
 18               '  (select max(b.timestamp_modify) '   ||
 19               '   from ' || cur_r.table_name         || ' b ' ||
 20               '   where b.id = a.id)';
 21      execute immediate l_str into l_id, l_timestamp_modify;
 22      pipe row(t_row(cur_r.table_name, l_id, l_timestamp_modify));
 23    end loop;
 24  end;
 25  /

Function created.

Testing:

SQL> select * from table(f_test);

TABLE_NAME                             ID TIMESTAMP_MODIFY
------------------------------ ---------- -------------------
TABA                                    1 24.10.2021 14:59:29
TAB_1                                   1 24.10.2021 15:03:16
TAB_2                                  25 24.10.2021 15:03:36
TEST                                    5 24.10.2021 15:04:24

SQL>
  • Related