查询以查找数据库的所有表中的值

时间:2015-12-04 08:23:01

标签: sql oracle

我在表中搜索一个特定的值并且有很多表,是否有查询在数据库的所有表中查找值,以便我可以快速找到该值而无需通过每个表一个之一。

我已经尝试了

SELECT owner, table_name, column_name FROM all_tab_columns WHERE column_name LIKE '%52871%';

SELECT * from dba_objects WHERE object_name like '%52871%'

1 个答案:

答案 0 :(得分:4)

您可以search for a VALUE in all COLUMNS of all TABLES in an entire SCHEMA使用XML SQL:

例如,我想在整个KING架构中的所有表的所有列中搜索值SCOTT

SQL> variable val varchar2(10)
SQL> exec :val := 'KING'

PL/SQL procedure successfully completed.

SQL> SELECT DISTINCT SUBSTR (:val, 1, 11) "Searchword",
  2    SUBSTR (table_name, 1, 14) "Table",
  3    SUBSTR (column_name, 1, 14) "Column"
  4  FROM cols,
  5    TABLE (xmlsequence (dbms_xmlgen.getxmltype ('select '
  6    || column_name
  7    || ' from '
  8    || table_name
  9    || ' where upper('
 10    || column_name
 11    || ') like upper(''%'
 12    || :val
 13    || '%'')' ).extract ('ROWSET/ROW/*') ) ) t
 14  ORDER BY "Table"
 15  /

Searchword  Table          Column
----------- -------------- --------------
KING        EMP            ENAME