在数据库的所有表中搜索值

Akm*_*hov 3 oracle find oracle10g

我需要在我的 Oracle 数据库中找到包含一些值的表。

我发现了两个查询。我尝试的第一个查询:

declare
  l_pattern varchar2(100) := '?? ?????? ??????? ????????';
  cursor cf is select table_name,column_name from user_tab_columns where data_type = 'VARCHAR2' order by table_name;

  t_str varchar2(2000) := 'select count(*) from dual where exists( select null from ';
  l_str varchar2(2000); 
  l_where_clause varchar2(2000) := ' where 1=0';
  l_last_table varchar2(100) := '';
  l_cnt number := 0;

  procedure query_ (i_txt varchar2) is
    l_txt varchar2(4000) := i_txt;
    l_ln number := length(l_txt);
    l_pieces number := ceil(l_ln/250);
  begin
   for i in 1..l_pieces loop
      dbms_output.put_line(substr(i_txt, 1+250*(i-1),least(250,l_ln-250*(i-1))));
    end loop;
    dbms_output.new_line;
  end;

begin
  for i in cf loop
    if l_last_table <> i.table_name then
    --  dbms_output.put_line(l_str||l_where_clause);
      execute immediate l_str || l_where_clause ||')' into l_cnt;
      if l_cnt > 0 then 
        query_ ('select * from ' || l_last_table || l_where_clause || ';');
      end if;
      l_cnt := 0;
      l_where_clause := ' where 1=0';
    end if;
    l_last_table := i.table_name;
    l_str := t_str || i.table_name;
    l_where_clause := l_where_clause || ' OR ' || i.column_name || ' like ''' ||l_pattern||'''';
   end loop;
  execute immediate l_str || l_where_clause ||')' into l_cnt;
  if l_cnt > 0 then 
    query_ ('select * from ' || l_last_table || l_where_clause || ';');
  end if;
end;
Run Code Online (Sandbox Code Playgroud)

这将返回错误:

Error starting at line 1 in command:
declare
...
end;

Error report: ORA-06502: PL/SQL: numeric or value error: character
string buffer too small ORA-06512: at line 35
06502. 00000 -  "PL/SQL: numeric or value error%s"
*Cause:    
*Action: 

select * from MV_CATALOG_ITEM where 1=0 OR CODE like '?? ??????
??????? ????????' OR UUID like '?? ?????? ??????? ????????' OR
TITLE like '?? ?????? ??????? ????????' OR PARENTITEMUUID like '??
?????? ??????? ????????' OR CATALOGTITLE like '?? ??????  ???????
????????' OR FOLDERTITLE like '?? ?????? ??????? ????????' OR
CATALOGUUID like '?? ?????? ??????? ????????' OR CATALOGCODE like '??
?????? ??????? ????????' OR FOLDERUUID like '?? ?????? ???????
????????';

select * from MV_HIERARCHICAL_TEMPLATES where 1=0 OR STRINGCONTENT2
like '?? ?????? ??????? ????????' OR STRINGCONTENT3 like '?? ??????
??????? ????????' OR IDENTIFIER like '?? ?????? ??????? ????????' OR
TEMPLATEUUID like '?? ?????? ??????? ???????? ' OR ATTRIBUTETITLE like
'?? ?????? ??????? ????????' OR STRINGCONTENT like '?? ?????? ???????
????????' OR GROUPTITLE like '?? ?????? ??????? ????????' OR INSTUUID
like '?? ?????? ??????? ????????' OR OBJUUID like '?? ?????? ???????
????????' OR TYP ECODE like '?? ?????? ??????? ????????' OR GROUPUUID
like '?? ?????? ??????? ????????';
Run Code Online (Sandbox Code Playgroud)

我尝试的第二个查询:

select table_name,
       column_name
  from( select table_name,
               column_name,
               to_number(
                 extractvalue(
                   xmltype(
                     dbms_xmlgen.getxml(
                       'select count(*) c from ' || table_name ||
                       ' where to_char(' || column_name || ') = ''JONES'''
                     )
                   ),
                   'ROWSET/ROW/C'
                 )
               ) cnt
          from (select utc.*, rownum
                  from user_tab_columns utc
                 where data_type in ('CHAR', 'VARCHAR2') ) )
 where cnt >= 0
Run Code Online (Sandbox Code Playgroud)

这将返回一个包含大量表和列以及错误的列表:

Error starting at line 1 in command: 
select table_name,
...
where cnt >= 0 

Error report:
SQL Error: ORA-19202: Error occurred in XML processing
ORA-00936: missing expression
ORA-06512: at "SYS.DBMS_XMLGEN", line 176
ORA-06512: at line 1
19202. 00000 -  "Error occurred in XML processing%s"
*Cause:    An error occurred when processing the XML function
*Action:   Check the given error message and fix the appropriate problem
Run Code Online (Sandbox Code Playgroud)

我自己也找不到办法。

Akm*_*hov 5

谢谢大家!以前的脚本执行得很慢。我用这个脚本得到了我的价值:

DECLARE
  match_count integer;
  v_search_string varchar2(4000) := 'advcgtfs000080000ict1mosqiomujrk';

BEGIN  
  FOR t IN (SELECT owner,
                   table_name, 
                   column_name 
              FROM all_tab_columns
             WHERE data_type in ('VARCHAR2') ) 
  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;
/
Run Code Online (Sandbox Code Playgroud)