How can I get column names from a table in SQL Server?

前端 未结 20 1311
既然无缘
既然无缘 2020-11-22 09:29

I want to query the name of all columns of a table. I found how to do this in:

  • Oracle
  • MySQL
  • PostgreSQL

But I also need to know:

相关标签:
20条回答
  • 2020-11-22 10:09

    It will check whether the given the table is Base Table.

    SELECT 
        T.TABLE_NAME AS 'TABLE NAME',
        C.COLUMN_NAME AS 'COLUMN NAME'
    FROM INFORMATION_SCHEMA.TABLES T
    INNER JOIN INFORMATION_SCHEMA.COLUMNS C ON T.TABLE_NAME=C.TABLE_NAME
        WHERE   T.TABLE_TYPE='BASE TABLE'
                AND T.TABLE_NAME LIKE 'Your Table Name'
    
    0 讨论(0)
  • 2020-11-22 10:11

    You can write this query to get column name and all details without using INFORMATION_SCHEMA in MySql :

    SHOW COLUMNS FROM database_Name.table_name;
    
    0 讨论(0)
  • 2020-11-22 10:11

    This SO question is missing the following approach :

    -- List down all columns of table 'Logging'
    select * from sys.all_columns where object_id = OBJECT_ID('Logging')
    
    0 讨论(0)
  • 2020-11-22 10:12

    you can use this query

    SELECT *
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME like N'%[ColumnName]%' and TABLE_NAME = N'[TableName]'
    
    0 讨论(0)
  • 2020-11-22 10:13

    One other option which is arguably more intuitive is:

    SELECT [name] 
    FROM sys.columns 
    WHERE object_id = OBJECT_ID('[yourSchemaType].[yourTableName]') 
    

    This gives you all your column names in a single column. If you care about other metadata, you can change edit the SELECT STATEMENT TO SELECT *.

    0 讨论(0)
  • 2020-11-22 10:15
    SELECT column_name, data_type, character_maximum_length, table_name,ordinal_position, is_nullable 
    FROM information_schema.COLUMNS WHERE table_name LIKE 'YOUR_TABLE_NAME'
    ORDER BY ordinal_position
    
    0 讨论(0)
提交回复
热议问题