2011-05-05 66 views
2

有没有办法知道SQL中的列数,类似count()...?SQL Server中的列计数

+0

的[为了获得在SQL表中的列的总数]可能重复(http://stackoverflow.com/questions/1464991/to-get-total- sql-table-in-sql中的列号) – 2013-06-11 19:28:35

回答

17

单程

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 
+0

按表格怎么样? ;) – Oded 2011-05-05 18:59:09

+0

增加了按表格分组的计数 – SQLMenace 2011-05-05 19:01:08

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

这应该跨多个RDBMS的工作:

 
select count(*) from INFORMATION_SCHEMA.COLUMNS 

如果你想获得幻想:

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

对于特定的表:

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

获取从这个OBJECT_ID,然后使用:

select count(*) from sys.columns 
where object_id = 831342026; 
0
SELECT count(*) 
FROM Database_Name.INFORMATION_SCHEMA.COLUMNS 
where table_name = 'Table_Name' 

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

from INFORMATION_SCHEMA.COLUMNS 

检索系统数据库只有