Find a column and a value from a schema scan all the table in oracle

筅森魡賤 提交于 2020-05-14 02:29:28

问题


I am trying to find a column (ABC) and it's value 1234 from a schema , basically i need to to check if ABC and a value from this column 1234 is present in any other table that is mapped to ABC , i tried to do a search the most efficient way but it is taking lot of time and not retrieving the desired result

i have tried https://lalitkumarb.wordpress.com/2015/01/06/sql-to-search-for-a-value-in-all-columns-of-all-atbles-in-an-entire-schema/

but the query is not results at all it is running running...


回答1:


You may write the output to a file if you get buffer overflow on set Serveroutput otherwise this should do.Output will have all tables that has 'ABC' column and respective count shows count of record with ABC column value as 1234.

SET SERVEROUTPUT ON 100000
DECLARE 
lv_count number(10):=0;
l_str    varchar2 (1000);
BEGIN 
FOR V1 IN 
(select distinct table_name 
 from dba_tab_columns 
 where column_name = 'ABC')

 LOOP

  BEGIN 
    lv_query := ' select count(*) from '||v1.table_name||' where ABC =1234';
    EXECUTE IMMEDIATE lv_query INTO lv_count;
    dbms_output.put_line(v1.table_name||' --> '||lv_count);

    EXCEPTION 
       WHEN OTHERS THEN 
         dbms_output.put_line('OTHERS EXCEPTION '||v1.table_name||' ERRCODE '||SQLERRM||' '||SUBSTR(SQLCODE,1,200));
    END; 

 END LOOP;

END;

To find all tables having column_name ABC, simple query as below should do.

select table_name 
     from dba_tab_columns 
     where column_name = UPPER('ABC');

PS: Metadata tables(dba_Tab_columns) stores column_name in upper case, to avoid any issues with case ,converting the case to upper for the literal.

Second query in PL/SQL block,

 SET SERVEROUTPUT ON 100000
    DECLARE 
    lv_count number(10):=0;
    l_str    varchar2 (1000);
    lv_col_name varchar2(255) :='ABC';

    BEGIN 
    FOR V1 IN 
    (select distinct table_name 
     from dba_tab_columns 
     where column_name = lv_col_name)

     LOOP
      dbms_output.put_line(lv_col_name||' '||v1.table_name);    
     END LOOP;

    END;


来源:https://stackoverflow.com/questions/61663260/find-a-column-and-a-value-from-a-schema-scan-all-the-table-in-oracle

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