Solved

Search All column in all Tables in Oracle 10g

Posted on 2008-10-08
15
5,043 Views
Last Modified: 2013-12-18
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?
0
Comment
Question by:michaelm702
  • 3
  • 3
  • 2
  • +3
15 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 32 total points
ID: 22675732
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
 

Author Comment

by:michaelm702
ID: 22675747
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22675867
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
 

Author Comment

by:michaelm702
ID: 22675938
When I execute the procedure I get an error saying that invalid character why? N i have columns have all type of datatypes
0
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 31 total points
ID: 22676024
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
 
LVL 42

Expert Comment

by:dqmq
ID: 22676027
BTW, that proc does not search numeric or date columns.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 15

Expert Comment

by:Shaju Kumbalath
ID: 22676125
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
 

Author Comment

by:michaelm702
ID: 22677437
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
 
LVL 15

Assisted Solution

by:Shaju Kumbalath
Shaju Kumbalath earned 31 total points
ID: 22677513
Create both the functions using sql plus
then
select  Search_Database( 'search_string') from dual;
 
replace "search_string" with ur keyword


0
 
LVL 10

Expert Comment

by:dbmullen
ID: 22678303
you want if any table has a given string in any column?
why?  what are you really trying to do?
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 31 total points
ID: 22680335
See attached.
search-db.txt
0
 
LVL 42

Expert Comment

by:dqmq
ID: 22684463
Missing end quote here:

      where datatype like '%CHAR%'
0
 
LVL 31

Expert Comment

by:awking00
ID: 22687903
dqmq,
Thanks for catching that. I broke my finger and my typing has gone to ....
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now