Home  >  Article  >  Database  >  根据 值 查找 表名 字段名

根据 值 查找 表名 字段名

WBOY
WBOYOriginal
2016-06-07 14:56:331402browse

假如你知道有一个值存在于 某个表 的 某个字段 中,你可以通过下面的SQL 查找出表名和字段名 http://my.oschina.net/jeffyu/blog Oracle DECLARE var_sql VARCHAR2(1000); var_value VARCHAR2(100):='要查的值'; var_cnt number; var_table_owner varchar2(2

假如你知道有一个值存在于 某个表 的 某个字段 中,你可以通过下面的SQL 查找出表名和字段名

http://my.oschina.net/jeffyu/blog

Oracle
DECLARE
    var_sql VARCHAR2(1000);
    var_value VARCHAR2(100):='要查的值';
    var_cnt number;
    var_table_owner varchar2(20):='表的所有者';
BEGIN
   
    For table_detail In (select table_name,column_name from all_tab_columns where owner=var_table_owner) Loop
        Begin
           var_sql := 'select count(*) from '||var_table_owner||'.' ||table_detail.table_name||' where trim('||table_detail.column_name||')='''||var_value||'''';
        execute immediate var_sql into var_cnt;
        exception when others then
                   var_cnt:=0;
      End;  
      if(var_cnt>0)then
         dbms_output.put_line('Table Name:'||table_detail.table_name||'-->Column Name:'||table_detail.column_name);
      end if;
        
    End Loop;
END;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn