Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Search All column in all Tables in Oracle 10g

Posted on 2008-10-08
15
Medium Priority
?
5,095 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 128 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 143

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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 124 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
 
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 124 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 32

Assisted Solution

by:awking00
awking00 earned 124 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 32

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Via a live example, show how to take different types of Oracle backups using RMAN.

927 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