计算SQL Server中的列数

时间:2011-05-05 18:56:42

标签: sql-server-2008 metadata

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

5 个答案:

答案 0 :(得分:18)

单程

select count(*) from sys.columns 

另一个

select count(*) from information_schema.columns

底部没有系统表

按表格

select count(*),table_name from information_schema.COLUMNS
GROUP BY table_name

仅限表格

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

仅限观看次数

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

答案 1 :(得分:6)

Select Count(*) From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME='YourTableName'

答案 2 :(得分:2)

这应该适用于多个RDBMS:

select count(*) from INFORMATION_SCHEMA.COLUMNS

如果你想得到幻想:

select TABLE_NAME,
       count(*)
  from INFORMATION_SCHEMA.COLUMNS
group by TABLE_NAME
order by TABLE_NAME

答案 3 :(得分:0)

对于特定的表:

select * from sys.tables
where name = 'mytable';

从中获取object_id,然后使用:

select count(*) from sys.columns
where object_id = 831342026;

答案 4 :(得分:0)

SELECT count(*)
FROM Database_Name.INFORMATION_SCHEMA.COLUMNS
where table_name = 'Table_Name'

需要运行2个步骤: 1-选择database_Name到您的数据库名称 2-更改您的表名称 注意:如果您没有像

那样编写database_Name
from INFORMATION_SCHEMA.COLUMNS 

仅检索系统数据库