Search All column in all Tables in Oracle 10g

I am using oracle 10g as my database and I dont know much in oracle.
I have a database which has 64 table & columns in it I want to search a particular string in all the columns and want to see which table & columns contains that value. My columns have all kinds of data like varchar, char, integer etc.
Is there a way of doing this?
michaelm702Asked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
there is no SELECT * FROM * WHERE * LIKE  syntax...
so, you will have to query each table individually, looking for each column.
you could help yourself by generating the queries using ALL_TAB_COLUMNS system view, though.
0
 
michaelm702Author Commented:
Angell - I have found the below procedure but it does not work for me and I get a invalid character error on running it. As I said i am not much aware of Oracle. Also I did not understand what you mean by "you could help yourself by generating the queries using ALL_TAB_COLUMNS system view, though"
Let me know how we do that.

create or replace procedure test_proc(l_search_string varchar2)
as
begin
 for rec in (select table_name from user_tables ) loop
  for rec2 in (select column_name from user_tab_columns where table_name = rec.table_name and DATA_TYPE in ('VARCHAR2', 'VARCHAR', 'CHAR')) loop
   execute immediate 'insert into str_search_log select '''||rec.table_name||
                     ''', rowid from '||rec.table_name||
                     ' where '||rec2.column_name||' like ''%'||l_search_string||'%''';
  end loop;
 end loop;
end;
/
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
the procedure you have is exactly that... doing the search dynamically.
however, it will only list the table and column names, and actually insert duplicates is the value is found multiple times...

anyhow, the procedure looks fine to me?...
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
michaelm702Author Commented:
When I execute the procedure I get an error saying that invalid character why? N i have columns have all type of datatypes
0
 
dqmqConnect With a Mentor Commented:
Just to be sure...

First, you need to create a table called  str_search_log, right?

Chances are you've encountered a table name that includes a special character or the like.  Try this:

  execute immediate 'insert into str_search_log select '''||rec.table_name||
                     ''', rowid from "'||rec.table_name||
                     '" where "'||rec2.column_name||'" like ''%'||l_search_string||'%''';

0
 
dqmqCommented:
BTW, that proc does not search numeric or date columns.
0
 
Shaju KumbalathDeputy General Manager - ITCommented:
create or replace function getlong( p_tname in varchar2,
p_cname in varchar2,
p_rowid in rowid ) return varchar2
as
l_cursor integer default dbms_sql.open_cursor;
l_n number;
l_long_val varchar2(4000);
l_long_len number;
l_buflen number := 4000;
l_curpos number := 0;
begin
dbms_sql.parse( l_cursor,
'select ' || p_cname || ' from ' || p_tname ||
' where rowid = :x',
dbms_sql.native );
dbms_sql.bind_variable( l_cursor, ':x', p_rowid );

dbms_sql.define_column_long(l_cursor, 1);
l_n := dbms_sql.execute(l_cursor);

if (dbms_sql.fetch_rows(l_cursor)>0)
then
dbms_sql.column_value_long(l_cursor, 1, l_buflen, l_curpos ,
l_long_val, l_long_len );
end if;
dbms_sql.close_cursor(l_cursor);
return l_long_val;
end getlong;
/
 
CREATE OR REPLACE FUNCTION Search_Database(
pt_string IN VARCHAR2) return VARCHAR2
IS
mt_table_name user_tab_columns.TABLE_NAME%type;
mt_column_name user_tab_columns.column_NAME%type;
mt_data_type user_tab_columns.data_type%type;
is_found NUMBER;
mt_sql varchar2(4000);
ret_sql VARCHAR2(4000);
P_STRING1 VARCHAR2(4000);
 
CURSOR get_table_column_list
IS
SELECT distinct table_name, column_name ,data_type
FROM user_tab_columns WHERE data_type in (' CHAR','VARCHAR2','RAW','LONG')
ORDER BY TABLE_NAME ;
 
 
 
BEGIN
OPEN get_table_column_list;
loop
FETCH get_table_column_list INTO mt_table_name, mt_column_name,mt_data_type;
EXIT WHEN get_table_column_list%NOTFOUND;
is_found:=0;
 
 
if mt_data_type='RAW' THEN
mt_sql:= 'SELECT COUNT(*) FROM '||mt_table_name||' WHERE '||mt_column_name||' LIKE '||CHR(39)||'%'||RAWTOHEX(utl_raw.cast_to_raw(pt_string))||'%'||CHR(39);

ELSIF mt_data_type='LONG' THEN
mt_sql:= 'SELECT COUNT(*) FROM '||mt_table_name||' WHERE getlong('||CHR(39)||mt_table_name||CHR(39)||','||CHR(39)||mt_column_name||CHR(39)||','||'ROWID)'||' LIKE '||CHR(39)||'%'||pt_string||'%'||CHR(39);
ELSE

mt_sql:= 'SELECT COUNT(*) FROM '||mt_table_name||' WHERE '||mt_column_name||' LIKE '||CHR(39)||'%'||pt_string||'%'||CHR(39);

END IF;
execute immediate mt_sql into is_found;
if is_found>0 then
ret_sql:= ret_sql||chr(13)||chr(10)||'Table: '||mt_table_name||' Col: '|| mt_column_name;
end if;
end loop;
CLOSE get_table_column_list;
RETURN ret_sql;
END Search_Database;  
0
 
michaelm702Author Commented:
shajukg that is very long can you explain how do I use it? I want to test it do I have to copy everything and execute in sql plus? Where do i put the string to be searched?
0
 
Shaju KumbalathConnect With a Mentor Deputy General Manager - ITCommented:
Create both the functions using sql plus
then
select  Search_Database( 'search_string') from dual;
 
replace "search_string" with ur keyword


0
 
dbmullenCommented:
you want if any table has a given string in any column?
why?  what are you really trying to do?
0
 
awking00Connect With a Mentor Commented:
See attached.
search-db.txt
0
 
dqmqCommented:
Missing end quote here:

      where datatype like '%CHAR%'
0
 
awking00Commented:
dqmq,
Thanks for catching that. I broke my finger and my typing has gone to ....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.