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

前端 未结 16 2262
一整个雨季
一整个雨季 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:37

    --it run completed -- no error

        SET SERVEROUTPUT ON SIZE 100000
    
    DECLARE
       v_match_count     INTEGER;
       v_counter         INTEGER;
    
    
    
    
    v_owner           VARCHAR2 (255) := 'VASOA';
    v_search_string   VARCHAR2 (4000) := '99999';
    v_data_type       VARCHAR2 (255) := 'CHAR';
    v_sql             CLOB := '';
    
    BEGIN
       FOR cur_tables
          IN (  SELECT owner, table_name
                  FROM all_tables
                 WHERE     owner = v_owner
                       AND table_name IN (SELECT table_name
                                            FROM all_tab_columns
                                           WHERE     owner = all_tables.owner
                                                 AND data_type LIKE
                                                           '%'
                                                        || UPPER (v_data_type)
                                                        || '%')
              ORDER BY table_name)
       LOOP
          v_counter := 0;
          v_sql := '';
    
          FOR cur_columns
             IN (SELECT column_name, table_name
                   FROM all_tab_columns
                  WHERE     owner = v_owner
                        AND table_name = cur_tables.table_name
                        AND data_type LIKE '%' || UPPER (v_data_type) || '%')
          LOOP
             IF v_counter > 0
             THEN
                v_sql := v_sql || ' or ';
             END IF;
    
             IF cur_columns.column_name is not null
             THEN
                v_sql :=
                      v_sql
                   || 'upper('
                   || cur_columns.column_name
                   || ') ='''
                   || UPPER (v_search_string)||'''';
    
                v_counter := v_counter + 1;
             END IF;
    
          END LOOP;
    
          IF v_sql is  null
          THEN
             v_sql :=
                   'select count(*) from '
                || v_owner
                || '.'
                || cur_tables.table_name;
    
          END IF;
    
          IF v_sql is not null
          THEN
             v_sql :=
                   'select count(*) from '
                || v_owner
                || '.'
                || cur_tables.table_name
                || ' where '
                || v_sql;
          END IF;
    
          --v_sql := 'select count(*) from ' ||v_owner||'.'|| cur_tables.table_name ||' where '||  v_sql;
    
    
          --dbms_output.put_line(v_sql);
          --DBMS_OUTPUT.put_line (v_sql);
    
          EXECUTE IMMEDIATE v_sql INTO v_match_count;
    
          IF v_match_count > 0
          THEN
            DBMS_OUTPUT.put_line (v_sql);
            dbms_output.put_line('Match in ' || cur_tables.owner || ': ' || cur_tables.table_name || ' - ' || v_match_count || ' records');
          END IF;
    
       END LOOP;
    EXCEPTION
       WHEN OTHERS
       THEN
          DBMS_OUTPUT.put_line (
                'Error when executing the following: '
             || DBMS_LOB.SUBSTR (v_sql, 32600));
    END;
    /
    

提交回复
热议问题