To get total number of columns in a table in sql

后端 未结 9 1316
清酒与你
清酒与你 2021-01-30 13:05

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

相关标签:
9条回答
  • 2021-01-30 13:11
    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'

    0 讨论(0)
  • 2021-01-30 13:13

    This query gets the columns name

    SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'YourTableName'
    

    And this one gets the count

    SELECT Count(*) FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'YourTableName'
    
    0 讨论(0)
  • 2021-01-30 13:15
    SELECT COUNT(COLUMN_NAME) 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_CATALOG = 'database' AND TABLE_SCHEMA = 'dbo'
    AND TABLE_NAME = 'table'     
    
    0 讨论(0)
  • 2021-01-30 13:17

    It can be done using:-

    SELECT COUNT(COLUMN_NAME) 'NO OF COLUMN' FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'Address'
    
    0 讨论(0)
  • 2021-01-30 13: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
    
    0 讨论(0)
  • 2021-01-30 13:25

    Correction to top query above, to allow to run from any database

    SELECT COUNT(COLUMN_NAME) FROM [*database*].INFORMATION_SCHEMA.COLUMNS WHERE 
    TABLE_CATALOG = 'database' AND TABLE_SCHEMA = 'dbo'
    AND TABLE_NAME = 'table'
    
    0 讨论(0)
提交回复
热议问题