To Get Total Number of Columns In a Table In SQL Server

Introduction

In this simple article, lets explore to get total number of columns in a table in SQL Server

Example

SELECT COUNT(COLUMN_NAME) AS [Number of Columns]
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_CATALOG = 'PracticalWorks' AND TABLE_SCHEMA = 'Demo'
AND TABLE_NAME = 'OrderDetails'  
Fig. 1 Number of Columns in a Table

From the INFORMATION_SCHEMA.COLUMNS table we can find the number of columns in a table.

  • TABLE_CATALOG – We need to pass the database name
  • TABLE_SCHEMA – We need to pass the schema name
  • TABLE_NAME – The name of the table which we are interested to get the number of columns

Conclusion

In this simple article, we discussed how to get total number of columns in a table. I assume this tip was very useful, and let’s explore more tips and tricks in our upcoming articles. Please share your feedback in the comment section.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Website Built with WordPress.com.

Up ↑

%d bloggers like this: