我想查询一个表的所有列的名称。我发现如何做到这一点:

甲骨文 MySQL PostgreSQL

但我也需要知道:如何在Microsoft SQL Server(在我的情况下是2008年)中做到这一点?


当前回答

你可以试试这个。这将给出所有列名及其各自的数据类型。

desc <TABLE NAME> ;

其他回答

在SQL Server 2008中可以使用sp_help。

sp_help <table_name>;

以上命令的键盘快捷键:选择表名(即突出显示它)并按ALT+F1。

--This is another variation used to document a large database for conversion (Edited to --remove static columns)

SELECT o.Name                   as Table_Name
     , c.Name                   as Field_Name
     , t.Name                   as Data_Type
     , t.length                 as Length_Size
     , t.prec                   as Precision_
FROM syscolumns c 
     INNER JOIN sysobjects o ON o.id = c.id
     LEFT JOIN  systypes t on t.xtype = c.xtype  
WHERE o.type = 'U' 
ORDER BY o.Name, c.Name

--In the left join, c.type is replaced by c.xtype to get varchar types
SELECT c.Name 
FROM sys.columns c
JOIN sys.objects o ON o.object_id = c.object_id
WHERE o.object_id = OBJECT_ID('TABLE_NAME')
ORDER BY c.Name

只需运行这个命令

EXEC sp_columns 'Your Table Name'

它将检查给定的表是否是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'