开发者

Count number of columns in SQL Server

开发者 https://www.devze.com 2023-03-03 10:14 出处:网络
Is there a way to know the number of columns in SQL, something like count(开发者_如何学Python)...?one way

Is there a way to know the number of columns in SQL, something like count(开发者_如何学Python)...?


one way

select count(*) from sys.columns 

another

select count(*) from information_schema.columns

The bottom one does not have the system tables

by table

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

tables only

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

views only

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


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

or if you need to specify the schema

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


This should work across multiple RDBMS's:

select count(*) from INFORMATION_SCHEMA.COLUMNS

And if you want to get fancy:

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


For a particular table:

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

Get the object_id from this, then use:

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


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

to Run 2 steps are needed : 1- choose the database_Name to your database name 2- Change Your Table Name Notes: if you didn't write database_Name like

from INFORMATION_SCHEMA.COLUMNS 

retrieves System database only

0

精彩评论

暂无评论...
验证码 换一张
取 消