Hi i have a requirement to find and identify a specific value from a fixed column across DWH schemas,ideally in order to identify the system name associated to each number so that i can track the total count of tables associated to that system an example will give a clear understanding
Let's consider i have some 100 systems starting from 1 to 100 numbers so each system is defined by a number
example
SYSTEM NO
LOGISTIC 1
FINANCE 2
SALES 3
CREDIT 4
SECUTIRY 5
and soo on untill 100
in DB i have some thousands of tables so each table will have s fixed column called ETL_SOURCE_SYSTEM which will have this number defined so that there will be a metadata static table/lookup table which will hold the system name and it's associated system no, so not that the system and system no is unique
so now i need to come up with a dynamic sql in order to scan through all the schemas in DWH in order to identify which system name and number have the total counts across all the schemas from all the tables
Query
select * from dba_tab_cols
where column_name like '%ETL_SOURCE_SYSTEM%'
Query - New Idea
select 'with tmp(schema_name,table_name, system_nbr) as (' from dual
union all
select 'select '''||owner||'''''as SCHEMA_NAME'','''||object_name||'''''as TABLE_NAME'',ETL_SOURCE_SYSTEM from '||object_name||' union all ' from dba_objects
where owner not in ('EXCLUDE the some of the nor relevant schemas like SYS,PUBLIC..')
--and owner=''
and object_type ='TABLE'
union all
select 'select '''','''',0 from dual) select owner,table_name,system_nbr from tmp order by owner,system_nbr desc ;' from dual;
If this is really possible to in dynamic sql in order to fetch the value system no 1 to 100 from all the tables in order to get the below desired result
Expected results
SCHEMA_NAME TOTAL_COUNTS_OF_TABLES SYSTEM_NAME SYSTEM_NUMBER
STAGE 150 LOGISTIC 1
EDW 300 LOGISTIC 1
MART 600 LOGISTIC 1
STAGE 150 FINANCE 2
EDW 300 FINANCE 2
MART 600 FINANCE 2
STAGE 150 SALES 3
EDW 300 SALES 3
MART 600 SALES 3
STAGE 150 CREDIT 4
EDW 300 CREDIT 4
MART 600 CREDIT 4
Note: DB is Oracle 11g and i have only covered few schemas for an example the schema list is big 50+ or more
any suggestions?
Dynamic SQL it is, as you presumed. Here's an example which shows how to do it. I presume you'll have to modify it a little bit (or a little bit more), but the principle should be OK.
For simplicity, I'm just displaying the output to the screen. If you want to do it differently, for example using a function which would return a collection or a refcursor, do so.
Read comments within code.
SQL> declare
2 l_cnt number; -- number of tables in each schema
3 l_tab varchar2(30); -- one (any; I chose MAX) table in each schema, as all of them contain ETL_SOURCE_SYSTEM column
4 l_ess number; -- value of ETL_SOURCE_SYSTEM in L_TAB table
5 l_sys varchar2(20); -- system name
6 l_out varchar2(200); -- output string
7 begin
8 -- display header
9 dbms_output.put_line('Schema Tabs System ESS' ||chr(10) ||
10 '----------- ---- ---------- ---');
11 -- loop through all users; I'm using only two users in my database
12 for cur_r in (select username from dba_users
13 where username in ('MICHAEL', 'SCOTT')
14 )
15 loop
16 -- pick any (I chose MAX) table name and count of tables for each schema
17 execute immediate 'select max(table_name), count(*) from dba_tables where owner = ' ||
18 chr(39) || cur_r.username || chr(39)
19 into l_tab, l_cnt;
20
21 -- fetch ETL_SOURCE_SYSTEM value in that schema
22 execute immediate 'select etl_source_system from ' || cur_r.username ||'.'|| l_tab into l_ess;
23
24 -- find system name in the lookup table (located in schema I use to run this code; should
25 -- be a privileged user which has access to DBA_ views
26 select system
27 into l_sys
28 from t_lookup
29 where etl_source_system = l_ess;
30
31 -- compose the output string
32 l_out := rpad(cur_r.username, 12, ' ') || to_char(l_cnt, '990') || ' ' ||
33 rpad(l_sys , 10, ' ') || to_char(l_ess, '990');
34
35 -- display the result
36 dbms_output.put_line(l_out);
37
38 end loop;
39 end;
40 /
Schema Tabs System ESS
----------- ---- ---------- ---
SCOTT 30 LOGISTIC 1
MICHAEL 27 FINANCE 2
PL/SQL procedure successfully completed.
SQL>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.