;

To get the count or list of columns of a table in SQL Server


Tutorialsrack 16/06/2019 SQL SQL Server

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.

Here are some questions:

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?

Here we get all the answers

Q1. How to get the list of all columns of the specific table?

Answer: To get the list of all the columns of a specific table of the database, a query is as follow:

Example - To get the list of all columns of the specific table
SELECT COLUMN_NAME FROM test_db.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='tbl_emp';

Q2. How to count the number of all columns of the specific table?

Answer: To count the number of all columns of the specific table, a query is as follow:

Example - To count the number of all columns of the specific table
SELECT COUNT(COLUMN_NAME) as 'Total No. of Columns' FROM test_db.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='tbl_emp';

Q3. How to get a single list of all columns of all table in the specific database?

Answer: To get a single list of all columns of all table in the specific database, a query is as follow:

Example - To get a single list of all columns of all table in the specific database
SELECT COLUMN_NAME, TABLE_SCHEMA, TABLE_NAME
FROM test_db.INFORMATION_SCHEMA.COLUMNS;

Q4. How to get a total number of columns of all table in the specific database?

Answer: To get a total number of columns of all table in the specific database, a query is as follow:

Example - To get a total number of columns of all table in the specific database
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!


Related Posts



Comments

Recent Posts
Tags