Search all columns, all tables for a specific value

后端 未结 1 332
甜味超标
甜味超标 2021-01-25 03:29

I know this is a duplicate question, but I couldn\'t find a way to reopen the discussion. Im trying to create a stored proc that will search all columns in all tables for a valu

1条回答
  •  广开言路
    2021-01-25 04:01

    See the comments:

    CLEAR SCREEN
    SET VERIFY OFF
    ACCEPT val CHAR PROMPT 'What value do you want to search for: '
    CLEAR SCREEN;
    
    DECLARE
        match_count                             INTEGER;
        v_search_string                         VARCHAR2(4000) := '&val'; /* this was <> */
    BEGIN
        FOR t IN (SELECT owner,
                         table_name,
                         column_name
                    FROM all_tab_columns
                   WHERE data_type IN ('CHAR',
                                       'VARCHAR2',
                                       'NCHAR',
                                       'NVARCHAR2',
                                       'CLOB',
                                       'NCLOB')) 
        LOOP
            BEGIN
                EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || t.owner || '.' || 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.owner || '.' || t.table_name || '     ' || t.column_name || ' ' || match_count);
                END IF;
            EXCEPTION
                WHEN OTHERS
                THEN
                    DBMS_OUTPUT.put_line('Error encountered trying to read ' || t.column_name || ' from ' || t.owner || '.' || t.table_name);
            END;
        END LOOP; 
    END;
    /     
    

    0 讨论(0)
提交回复
热议问题