计算SQL Server中的列数

cMi*_*nor 2 metadata sql-server-2008

有没有办法知道SQL中的列数,比如count()......?

SQL*_*ace 18

单程

select count(*) from sys.columns 
Run Code Online (Sandbox Code Playgroud)

另一个

select count(*) from information_schema.columns
Run Code Online (Sandbox Code Playgroud)

底部没有系统表

靠桌子

select count(*),table_name from information_schema.COLUMNS
GROUP BY table_name
Run Code Online (Sandbox Code Playgroud)

仅限表格

select count(*),c.table_name 
from information_schema.COLUMNS c
JOIN information_schema.tables t ON c.TABLE_NAME = t.TABLE_NAME
AND c.TABLE_Schema = t.TABLE_Schema
WHERE TABLE_TYPE = 'base table'  
GROUP BY c.table_name
Run Code Online (Sandbox Code Playgroud)

只有意见

select count(*),c.table_name 
from information_schema.COLUMNS c
JOIN information_schema.tables t ON c.TABLE_NAME = t.TABLE_NAME
AND c.TABLE_Schema = t.TABLE_Schema
WHERE TABLE_TYPE = 'view'  
GROUP BY c.table_name
Run Code Online (Sandbox Code Playgroud)


Dus*_*ges 6

Select Count(*) From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME='YourTableName'
Run Code Online (Sandbox Code Playgroud)