Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DB2: How do I find if a column is present in a table or list of tables?

Tags:

sql

db2

Im using a DB2 Database. What would be a suitable SQL query to find out if a column is exists in a table or list of tables?

e.g

if "column_name" is found in "table name" or [list of table names]

return true or the name of tables that have that column.

Many thanks.

like image 225
Mo. Avatar asked Jan 05 '12 14:01

Mo.


2 Answers

Tested on DB2 z/OS 9.1 and LUW 9.7:

SELECT STRIP(TBCREATOR) || '.' || STRIP(TBNAME)
FROM SYSIBM.SYSCOLUMNS 
WHERE NAME = 'your_col'
  AND TBNAME IN ('list', 'of', 'tables')

If you only want results from a specific schema you might add AND TBCREATOR = 'your_schema' to the end of the query.

like image 89
bhamby Avatar answered Sep 30 '22 14:09

bhamby


Use SYSCAT.COLUMNS catalog view:

SELECT TABNAME
FROM SYSCAT.COLUMNS
WHERE 
    TABNAME IN ('table name 1', 'table name 2') AND 
    COLNAME = 'column_name';
like image 23
Michał Powaga Avatar answered Sep 30 '22 13:09

Michał Powaga