Oraclesearch所有表的所有列的string
我需要在我们的oracle数据库中search所有表和列中的string。 我有我在网上find的下面的查询,但是当我执行它时,我得到以下错误
任何帮助表示赞赏
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; /
至less,您需要查询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; /
然而,如果你正在寻找一个string,你几乎可以肯定地想限制自己寻找可以存储string的列。 例如,在DATE列中search一个string是没有意义的。 除非您对BLOB列包含的内容以及parsingBLOB列的二进制格式有很多先验知识,否则在BLOB列中searchstring是没有意义的。 鉴于此,我怀疑你想要更多的东西
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 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; /
当然,这将会非常缓慢 – 对于表格中的每个string列,您都要扫描每个表格一次。 用适度大的表格和中等数量的string列,这可能需要相当长的一段时间。