1

Can I write an sql query that can find the names of all tables in a database that have column name like %COL_FAX_ID%. If so, how?

Database used is oracle or db2.

Vamsi Emani
  • 10,072
  • 9
  • 44
  • 71

6 Answers6

6

For Oracle, you could try:

SELECT owner,
       table_name,
       column_name
  FROM all_tab_cols
 WHERE column_name LIKE '%COL_FAX_ID%'
 ORDER BY owner,
          table_name;

For a full list of the Oracle data dictionary views etc. see here.

Hope it helps...

Ollie
  • 17,058
  • 7
  • 48
  • 59
2

I don't have an oracle install lying around to test this with but you should be able to do something like:

SELECT TABLE_NAME
FROM ALL_TAB_COLUMNS
WHERE COLUMN_NAME LIKE '%COL_FAX_ID%'
  • 1
    I'd use ALL_TAB_COLS rather than ALL_TAB_COLUMNS as per the Oracle definition: This view (ALL_TAB_COLS) differs from "ALL_TAB_COLUMNS" in that hidden columns are not filtered out – Ollie Aug 22 '12 at 08:31
0

Dublicate of possible How to find all the tables in MySQL with specific column names in them?

Answer:

SELECT DISTINCT TABLE_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME IN ('columnA','ColumnB')
        AND TABLE_SCHEMA='YourDatabase';

Edit: Sorry I Miss saw the sql for beeing mysql tag... But this may work aswell? Dunno. Gl & Hf

Community
  • 1
  • 1
Breezer
  • 10,410
  • 6
  • 29
  • 50
0
select distinct table_name from all_tab_columns where column_name like %'COL_FAX_ID%' 
Alexander Tokarev
  • 1,000
  • 7
  • 16
  • I'd use `ALL_TAB_COLS` rather than `ALL_TAB_COLUMNS` as per the Oracle definition: This view (ALL_TAB_COLS) differs from "ALL_TAB_COLUMNS" in that hidden columns are not filtered out. – Ollie Aug 22 '12 at 08:30
  • 1
    Regarding the author would like to get table name all_tab_columns is fine as well but many thanks at any case – Alexander Tokarev Aug 22 '12 at 09:06
0

For oracle:

SELECT table_name,column_name from all_tab_columns 
where column_name like '%COL_FAX_ID%'
valex
  • 23,966
  • 7
  • 43
  • 60
0

For DB2, you will want to use the SYSCAT.COLUMNS catalog view.

SELECT *
FROM SYSCAT.COLUMNS
WHERE COLNAME LIKE '%COL_FAX_ID%'
bhamby
  • 15,112
  • 1
  • 45
  • 66