To get total number of columns in a table in sql

2020-05-13 09:45发布

I need a query in sql to get total columns in a table.Can anybody help?

标签: sql-server
9条回答
走好不送
2楼-- · 2020-05-13 10:16
Select Table_Name, Count(*) As ColumnCount
From Information_Schema.Columns
Group By Table_Name
Order By Table_Name

This code show a list of tables with a number of columns present in that table for a database.

If you want to know the number of column for a particular table in a database then simply use where clause e.g. where Table_Name='name_your_table'

查看更多
forever°为你锁心
3楼-- · 2020-05-13 10:18
SELECT COUNT(COLUMN_NAME) 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_CATALOG = 'database' AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'table'     
查看更多
啃猪蹄的小仙女
4楼-- · 2020-05-13 10:18

The below query will display all the tables and corresponding column count in a database schema

SELECT Table_Name, count(*) as [No.of Columns]
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'dbo' -- schema name
group by table_name
查看更多
登录 后发表回答