在Oracle

时间:2015-06-04 05:11:42

标签: sql oracle search metadata

如何根据i wnat中给出的值检索列名以获取oracle中的column_name

就像我不知道一个名字,但我知道列值

我试过这种方式

select column_name from table_name where column_value=XXXXX;   

2 个答案:

答案 0 :(得分:1)

试试这样:

SQL> select table_name, 
       column_name, 
       :search_string search_string, 
       result
  from cols, 
       xmltable(('ora:view("'||table_name||'")/ROW/'||column_name||'[ora:contains(text(),"%'|| :search_string || '%") > 0]')
       columns result varchar2(10) path '.'
       )
where table_name in ('EMP', 'DEPT')

Source

答案 1 :(得分:0)

如果您想在纯 SQL 中执行此操作,则可以使用 XML 方法。

例如,要在 SCOTT 架构中搜索 KING 值:

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

SQL>

阅读SQL to Search for a VALUE in all COLUMNS of all TABLES in an entire SCHEMA