简体   繁体   中英

Db2: Query for searching table with different column name

In my database, all tables should have a column (let's say "abc") and I want to find out the tables which do not have this column. Do we have any such query to fulfill this requirement?

Database: Db2 v11.1 LUW

You can build a query against SYSCAT.COLUMNS (and SYSCAT.TABLES) to find those tables not having such column:

select tabname from syscat.tables t1
where not exists
    (select colname from syscat.columns c
     where c.tabname=t1.tabname and colname='foo')
and tabname like 'SYSX%'

Above is just an example and not optimized.

Non-system tables only. Column name must be in uppercase, unless you specified the column name as “abc” (in double quotes) upon the table creation intentionally.

select tabschema, tabname 
from syscat.tables t
where not exists
(
select 1 
from syscat.columns c
where c.tabschema=t.tabschema and c.tabname=t.tabname 
and c.colname='ABC'
)
and tabschema not like 'SYS%'
and type='T';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM