In this article, we will learn how to get the count or list of columns of a table exists in your database in SQL Server.
Q1. How to get the list of all columns of the specific table?
Q2. How to count the number of all columns of the specific table?
Q3. How to get a single list of all columns of all table in the specific database?
Q4. How to get a total number of columns of all table in the specific database?
Answer: To get the list of all the columns of a specific table of the database, a query is as follow:
SELECT COLUMN_NAME FROM test_db.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='tbl_emp';
Answer: To count the number of all columns of the specific table, a query is as follow:
SELECT COUNT(COLUMN_NAME) as 'Total No. of Columns' FROM test_db.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='tbl_emp';
Answer: To get a single list of all columns of all table in the specific database, a query is as follow:
SELECT COLUMN_NAME, TABLE_SCHEMA, TABLE_NAME
FROM test_db.INFORMATION_SCHEMA.COLUMNS;
Answer: To get a total number of columns of all table in the specific database, a query is as follow:
SELECT 'COLUMN NAME' = COUNT(*), TABLE_SCHEMA, TABLE_NAME
FROM test_db.INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_SCHEMA, TABLE_NAME;
I hope this article will help you to understand to get the count or list of columns of a table present in a database in SQL Server.
Share your valuable feedback and help us to improve. If you find anything incorrect, or you want to share more information about the topic discussed above. please post your comment at the bottom of this article. Thank you!
Comments