Monday, January 30, 2012

Count Column and Count Index on table basis

Today I just need to know about column count and index count for particular table. I have found following queries to do this.


Here tbl_customer is the table name I have used.


Query for the Column Count:


SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME in ('tbl_customer')


Query for the Index Count with Index name and key count:




SELECT object_name(i.object_id) as TableName, name as IndexName, KeyCount FROM sys.indexes i inner join
(select COUNT(*) as KeyCount, object_id, index_id 
from sys.index_columns where is_included_column = 0 group by object_id, index_id) ic
on i.object_id=ic.object_id and i.index_id = ic.index_id where object_name(i.object_id)='tbl_customer'


Suggestions are always welcome.


Thanks.