Search All Fields In All Tables For A Specific Value (Oracle)

前端 未结 16 2081
一整个雨季
一整个雨季 2020-11-22 01:05

Is it possible to search every field of every table for a particular value in Oracle?

There are hundreds of tables with thousands of rows in some tables so I know th

16条回答
  •  囚心锁ツ
    2020-11-22 01:39

    I did some modification to the above code to make it work faster if you are searching in only one owner. You just have to change the 3 variables v_owner, v_data_type and v_search_string to fit what you are searching for.

    SET SERVEROUTPUT ON SIZE 100000
    
    DECLARE
      match_count INTEGER;
    -- Type the owner of the tables you are looking at
      v_owner VARCHAR2(255) :='ENTER_USERNAME_HERE';
    
    -- Type the data type you are look at (in CAPITAL)
    -- VARCHAR2, NUMBER, etc.
      v_data_type VARCHAR2(255) :='VARCHAR2';
    
    -- Type the string you are looking at
      v_search_string VARCHAR2(4000) :='string to search here...';
    
    BEGIN
      FOR t IN (SELECT table_name, column_name FROM all_tab_cols where owner=v_owner and data_type = v_data_type) LOOP
    
        EXECUTE IMMEDIATE 
        'SELECT COUNT(*) FROM '||t.table_name||' WHERE '||t.column_name||' = :1'
        INTO match_count
        USING v_search_string;
    
        IF match_count > 0 THEN
          dbms_output.put_line( t.table_name ||' '||t.column_name||' '||match_count );
        END IF;
    
      END LOOP;
    END;
    /
    

提交回复
热议问题