Monday, June 18, 2012

Useful DB2 Queries

ALL TABLES:
select * from sysibm.systables

TABLE DESCRIPTION:
select * from sysibm.syscolumns where tbname = 'tableName'
- NOT VERY RELIABLE, SOME COLs MIGHT BE MISSING

or

select colname, typename, length, scale from syscat.columns
where tabschema = 'schemaName' and tabname = 'tableName'
- NOT VERY RELIABLE, SOME COLs MIGHT BE MISSING

or

describe table tableName - DOES NOT WORK :(

Note: Use SYSCAT views rather than the SYSIBM tables. Reason being is that from release to release the tables are being revised while DB2 development tries hard to keep the SYSCAT view signatures compatible. For this reason SYSCAT is being described in the SQL Reference while SYSIBM is not.

GET CURRENT DATE/TIME:
select current date from sysibm.sysdummy1
select current time from sysibm.sysdummy1
select current timestamp from sysibm.sysdummy1

Note: sysibm.sysdummy1 table is a special in-memory table that can be used to discover the value of DB2 registers

Sources:
http://www.databaseteam.org/9-db2/b4ac3f33bec9fb23.htm#.UfdJQ41OS6M
http://www.ibm.com/developerworks/data/library/techarticle/0211yip/0211yip3.html