What is the SQL command to return the field names of a table?

后端 未结 12 1201
野的像风
野的像风 2020-12-05 07:03

Say I have a table called myTable. What is the SQL command to return all of the field names of this table? If the answer is database specific then I need SQL Server right

12条回答
  •  萌比男神i
    2020-12-05 07:26

    SQL-92 standard defines INFORMATION_SCHEMA which conforming rdbms's like MS SQL Server support. The following works for MS SQL Server 2000/2005/2008 and MySql 5 and above

    select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'myTable'
    

    MS SQl Server Specific:

    exec sp_help 'myTable'
    

    This solution returns several result sets within which is the information you desire, where as the former gives you exactly what you want.

    Also just for completeness you can query the sys tables directly. This is not recommended as the schema can change between versions of SQL Server and INFORMATION_SCHEMA is a layer of abstraction above these tables. But here it is anyway for SQL Server 2000

    select [name] from dbo.syscolumns where id = object_id(N'[dbo].[myTable]')
    

提交回复
热议问题