0

I want a query in oracle to get the column name from the table by passing value. Means that In most of the case - We write the query like that - select * from table where column = 'value'. But in my case i don't know the column name.

Can any one suggest me. Thanks in advance...

MakDeveloper
  • 31
  • 1
  • 1
  • 3
  • select * from user_tables – Chintan Gor Jun 23 '16 at 09:59
  • select table_name from user_Tables where table_name = 'school_master'; – Chintan Gor Jun 23 '16 at 10:00
  • I have a value like 'Maria' and i want to search the column name and table name from the oracle database – MakDeveloper Jun 23 '16 at 10:13
  • 1
    You want to find the name of any column (in any table?) that contains that value? If so your question isn't clear; and [possible duplicate](http://stackoverflow.com/q/208493/266304). – Alex Poole Jun 23 '16 at 10:19
  • Is 'Maria' a value contained in one or more of these unknown columns? How come you don't know the name of the column to look in? Is this some homework problem or something? – Boneist Jun 23 '16 at 10:20
  • Yes,'Maria' is a value contained in one or more of these unknown columns. Can you please help me for that – MakDeveloper Jun 23 '16 at 10:25
  • @MakDeveloper - try this http://stackoverflow.com/questions/208493/search-all-fields-in-all-tables-for-a-specific-value-oracle – vercelli Jun 23 '16 at 10:29
  • If I understand your question. Its like you have a data and you have to search it throughout the database of each and evry table and list down the tale name and column name. – Avrajit Roy Jun 23 '16 at 10:41
  • Yes, You are right.... Please help me for that – MakDeveloper Jun 23 '16 at 10:50

2 Answers2

0

You can try to build a dynamic query to check all the tables of your DB.

setup:

create table tab1 ( v1 varchar2(100), n1 number, v1b varchar2(100));
create table tab2 ( v2 varchar2(100), n2 number, v2b varchar2(100));
create table tab3 ( v3 varchar2(100), n3 number, v3b varchar2(100));
insert into tab1 values ('Maria', 1, 'aa');
insert into tab1 values ('xx', 2, 'bb');
insert into tab2 values ('yy', 3, 'Maria');
insert into tab2 values ('zz', 3, 'cc');
insert into tab3 values ('WW', 4, 'DD');

build the dynamic query:

select 'select table_name,
               matches from (' || listagg(statement, ' UNION ALL ') within group (order by table_name) || ')
        where matches > 0'
from (     
    select 'select ''' || table_name ||
             ''' as TABLE_NAME, count(1) as MATCHES from ' || table_name || ' WHERE ' ||
             listagg(column_name || ' = ''Maria''', ' OR ') within group (order by column_name) as statement,
            table_name
        from user_tab_columns col
        where data_type = 'VARCHAR2'
        group by table_name
     )

This will return a query, that you can run to check all the tables; in my example, this will build the query (not formatted) :

SELECT table_name, matches
  FROM (SELECT 'TAB1' AS TABLE_NAME, COUNT(1) AS MATCHES
          FROM TAB1
         WHERE    V1 = 'Maria'
               OR V1B = 'Maria'
        UNION ALL
        SELECT 'TAB2' AS TABLE_NAME, COUNT(1) AS MATCHES
          FROM TAB2
         WHERE    V2 = 'Maria'
               OR V2B = 'Maria'
        UNION ALL
        SELECT 'TAB3' AS TABLE_NAME, COUNT(1) AS MATCHES
          FROM TAB3
         WHERE    V3 = 'Maria'
               OR V3B = 'Maria')
 WHERE matches > 0;

Running this query will give:

TABL    MATCHES
---- ----------
TAB1          1
TAB2          1

Please notice that I used USER_TAB_COLUMNS, thus searching only in the tables of the login schema; if you want to search in different schemas, you can use ALL_TAB_COLUMNS or DBA_TAB_COLUMNS, depending on what you need and on the privileges of you user; see here for something more.

Also, consider that USER_TAB_COLUMNS will get the colums of tables and views; if you want to limit your search to tables, you can join USER_TAB_COLUMNS(ALL_TAB_COLUMNS, DBA_TAB_COLUMNS) to USER_TABLES (ALL_TABLES, DBA_TABLES) by TABLE_NAME, or TABLE_NAME and OWNER If you decide to use ALL or DBA tables:

SQL> create view vTab1 as select * from tab1;

View created.

SQL> select count(1)
  2  from user_tab_columns
  3  where table_name = 'VTAB1';

  COUNT(1)
----------
         3

SQL> select count(1)
  2  from user_tab_columns
  3       inner join user_tables using(table_name)
  4  where table_name = 'VTAB1';

  COUNT(1)
----------
         0

SQL>
Aleksej
  • 22,443
  • 5
  • 33
  • 38
-2
select table_name from user_Tables where table_name = 'bogus';
Chintan Gor
  • 1,062
  • 2
  • 15
  • 35
  • I have a value like 'Maria' and i want to search the column name and table name from the oracle database – MakDeveloper Jun 23 '16 at 10:18
  • The OP is asking for a the column name given a value. Your answer does will just give them the table name which they already have. – Pallas Apr 05 '19 at 14:49