473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to search all columns of all tables in a database for a keyword?

Dear Team,

i have an requirement that : i want to search all the columns of all the tables in the particular database based on the specific key word or an free text.

example :

table 1: columns data
empname sam
empid 01

table 2 columns data
deptname sam
departmentid 10

table 3 columns data
organization name sam
organization id 1

when i search for text " SAM"

it should search me from the entire database, all tables and columns of it and display the result

output : tablename cloumn value
table1 empname sam
table2 deptname sam
table3 organizationame sam

the example is just an sample not the real data .

please help me with sample code or any link related to it .


thanks in advance
Dec 14 '08 #1
4 15742
debasisdas
8,127 Expert 4TB
you need to use cursor for that using a number of OR conditions.
Dec 14 '08 #2
Pilgrim333
127 100+
Hi,

You can also try to write a procedure to do this for you. I think this would be easier. Make use of the all_tables view etc.

Pilgrim.
Dec 14 '08 #3
Can you please provide any link that will help me out or any code . thanx in advance
Dec 15 '08 #4
amitpatel66
2,367 Expert 2GB
The following code would help you to proceed further. It works fine for columns of data type number,varchar,char etc but not for LONG.

You will need to check the following:

1. If you want to check for all the tables of a database then you need to make sure that SELECT for all the tables is GRANTED to the schema from where you are running this code
2. Convert LONG TO VARCHAR explicitly and then do the comparison.

Expand|Select|Wrap|Line Numbers
  1. declare
  2. cursor c1 is select table_name from all_tables WHERE owner = (SELECT user FROM dual);
  3. col_names SYS_REFCURSOR;
  4. TYPE dat_res IS RECORD(tab_name VARCHAR2(100),col_name VARCHAR2(100));
  5. TYPE dr is table of dat_res;
  6. act_dat dr:= dr();
  7. status NUMBER := 0;
  8. cnt NUMBER := 1;
  9. sql_stmt VARCHAR2(10000);
  10. tab_name1 VARCHAR2(100);
  11. col_name1 VARCHAR2(100);
  12. BEGIN
  13. FOR I IN C1 LOOP
  14. sql_stmt:= 'SELECT table_name,column_name FROM all_Tab_cols WHERE table_name = '||CHR(39)||I.table_name||CHR(39);
  15. OPEN col_names FOR sql_stmt;
  16. LOOP
  17. FETCH col_names INTO tab_name1,col_name1;
  18. EXIT WHEN col_names%NOTFOUND;
  19. BEGIN
  20. EXECUTE IMMEDIATE 'SELECT 1 FROM '||tab_name1|| ' WHERE '||'TO_CHAR('||col_name1||') LIKE '||CHR(39)||'BCD'||CHR(39) INTO status;
  21. EXCEPTION
  22. WHEN NO_DATA_FOUND THEN
  23. NULL;
  24. WHEN OTHERS THEN
  25. DBMS_OUTPUT.PUT_LINE('Error In table: '||tab_name1||' Column:'||col_name1||' '||SQLERRM);
  26. END;
  27. IF (status = 1) THEN
  28. act_dat.extend;
  29. act_dat(cnt).tab_name:= tab_name1;
  30. act_dat(cnt).col_name:= col_name1;
  31. status := 0;
  32. cnt:= cnt + 1;
  33. END IF;
  34. END LOOP;
  35. CLOSE col_names;
  36. END LOOP;
  37. dbms_output.put_line('Table Name : Column Name');
  38. FOR K IN act_dat.FIRST..act_dat.LAST LOOP
  39. dbms_output.put_line(act_dat(K).tab_name||' : '||act_dat(K).col_name);
  40. END LOOP;
  41. END;
  42.  
  43. SQL> /
  44. Error In table: PLAN_TABLE Column:OTHER ORA-00932: inconsistent datatypes: expected CHAR got LONG
  45. Error In table: PLAN_TABLE Column:OTHER ORA-00932: inconsistent datatypes: expected CHAR got LONG
  46. Table Name : Column Name
  47. EMP : EMPNAME
  48. EMP1 : EMPNAME
  49.  
  50. PL/SQL procedure successfully completed.
  51.  
  52. SQL> 
  53.  
  54.  
If you see above , it list the tables that were errored out due to column datatype LONG and the list of tables that contain the value 'BCD'

Just BUILD on this and create your code as per your requirement
Dec 15 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Millie Niss | last post by:
I am new to PHP and mySQL, and I am trying to build a database of services (on a service table) provided by agencies (another table), where one agency can provide many services, but a service...
1
by: Nikola Pecigos | last post by:
Hi, I have the following problem: We have an Oracle 9.2 with one table "document" which contains a path to the filesystem. If I want to index these files (HTML, PDF, World, Excel, etc.), I...
2
by: Ryan | last post by:
I'm looking for a stored procedure (or query) to search an entire database for a specific string value and replace it with another. I'm sure I saw an SP for this a while back by someone, but cannot...
2
by: Teknik & System | last post by:
Hi, I need to search all tables and all records in a large database (v. 7.2) for at text string. Are there any _easy_ ways of doing this? TIA! Jakob
4
by: Gobi | last post by:
Hello, I have a Database with lists of Clients in each. Every year a new tables is created with the naming convention "CloseYear" ie close1999, close2000 There are tables from this year back to...
3
by: wsnyder3 | last post by:
I am trying to create a search where the user can type in a word and if there are any matching word(s) on any table that it will display the tables matches. I am not real familiar with Access I am...
1
by: atl10spro | last post by:
Hello Everyone, I am new to MS Access and although I have created several different databases I lack the VB knowledge to code a search function. I am turning to your expertise for assistance. ...
2
by: Sid Price | last post by:
I have an application that has already been deployed and we need to add a new column to some of the tables in the application database. We are using an access database. We have a solution working...
0
by: SamKhazary | last post by:
I have created a database that has a main table with a description of different products. I have 5 other data bases that are linked sheets that have returns for the 5 different prduct groups. I'd...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.