Count total number of columns in a table in sql server

Today we will check how to count total number of columns in a table in sql server. Sometimes we need to compare the columns of one table on multiple servers to get idea if anything is missing or not. It’s better to query the database rather than manually count it.
Its quite simple
SELECT COUNT(COLUMN_NAME)as [Total Columns] FROM INFORMATION_SCHEMA.COLUMNS WHERE 
TABLE_CATALOG = 'database_name_will_come_here' -- IF YOU WANT TO QUERY ANY OTHER DATABASE
AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'table_name_will_come_here'

That's it. Hopefully it will work for you.

0 comments: