Find the number of columns in a table

前端 未结 19 2182
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:40

    Well I tried Nathan Koop's answer and it didn't work for me. I changed it to the following and it did work:

    SELECT COUNT(*)
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE table_name = 'table_name'
    

    It also didn't work if I put USE 'database_name' nor WHERE table_catalog = 'database_name' AND table_name' = 'table_name'. I actually will be happy to know why.

    0 讨论(0)
  • 2020-11-27 11:40
    SELECT COUNT(*) 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE 
       TABLE_NAME = 'table_name';
    
    0 讨论(0)
  • 2020-11-27 11:40

    Following query finds how columns in table:-

     SELECT COUNT(COLUMN_NAME) FROM USER_TAB_COLUMNS
     WHERE TABLE_NAME = 'TableName';
    
    0 讨论(0)
  • 2020-11-27 11:41
    SELECT TABLE_SCHEMA
        , TABLE_NAME
        , number = COUNT(*) 
    FROM INFORMATION_SCHEMA.COLUMNS
    GROUP BY TABLE_SCHEMA, TABLE_NAME;
    

    This one worked for me.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-27 11:48
    SELECT count(*)
    FROM information_schema.columns
    WHERE table_name = 'Your_table_name';
    

    Note: Your_table_name should be replaced by your actual table name

    0 讨论(0)
提交回复
热议问题