Find the number of columns in a table

前端 未结 19 2329
Happy的楠姐
Happy的楠姐 2020-11-27 11:13

It is possible to find the number of rows in a table:

select count(*) from tablename

Is it possible to find the number of columns in a tabl

19条回答
  •  无人及你
    2020-11-27 11:44

    Since all answers are using COUNT(), you can also use MAX() to get the number of columns in a specific table as

    SELECT MAX(ORDINAL_POSITION) NumberOfColumnsInTable
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_CATALOG = 'YourDatabaseNameHere'
          AND 
          TABLE_SCHEMA = 'YourSchemaNameHere'
          AND
          TABLE_NAME = 'YourTableNameHere';
    

    See The INFORMATION_SCHEMA COLUMNS Table

提交回复
热议问题