Oracle搜索所有表所有列的字符串

前端之家收集整理的这篇文章主要介绍了Oracle搜索所有表所有列的字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Search All Fields In All Tables For A Specific Value (Oracle)12个
我需要在所有表和列中搜索我们的oracle数据库中的字符串.我有下面的查询我发现在线,但是当我执行它,我得到以下错误

任何帮助是赞赏

ORA-06550: line 6,column 31:
PL/sql: ORA-00904: "COLUMN_NAME": invalid identifier
ORA-06550: line 6,column 12:
PL/sql: sql Statement ignored
ORA-06550: line 8,column 30:
PLS-00364: loop index variable 'T' use is invalid
ORA-06550: line 7,column 4:
PL/sql: Statement ignored
ORA-06550: line 12,column 38:
PLS-00364: loop index variable 'T' use is invalid
ORA-06550: line 12,column 16:
PL/sql: Statement ignored

BEGIN  
  FOR t IN (SELECT table_name,column_name FROM all_tables) 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;
/
至少您需要查询ALL_TAB_COLUMNS,而不是ALL_TABLES
DECLARE
  match_count integer;
  v_search_string varchar2(4000) := <<string you want to search for>>;
BEGIN  
  FOR t IN (SELECT owner,table_name,column_name FROM all_tab_columns) LOOP   
    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; 
  END LOOP;
END;
/

但是,如果您正在寻找一个字符串,您几乎肯定会限制自己寻找可以存储字符串的列.例如,在DATE列中搜索字符串是没有意义的.除非您对BLOB列的含义有很多先验知识,还有解析BLOB列的二进制格式的功能,那么在BLOB列中搜索字符串是没有意义的.鉴于此,我怀疑你想要更像一些东西

DECLARE
  match_count integer;
  v_search_string varchar2(4000) := <<string you want to search for>>;
BEGIN  
  FOR t IN (SELECT owner,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;
/

当然,这将是非常缓慢的 – 你可以对表中的每个字符串列全面扫描一次表.使用中等大的桌子和中等数量的字符串列,这可能需要相当长的一段时间.

原文链接:https://www.f2er.com/oracle/205592.html

猜你在找的Oracle相关文章