What is the equivalent of 'describe table' in SQL Server?

前端 未结 23 1082
一整个雨季
一整个雨季 2020-11-27 09:12

I have a SQL Server database and I want to know what columns and types it has. I\'d prefer to do this through a query rather than using a GUI like Enterprise Manager. Is the

23条回答
  •  囚心锁ツ
    2020-11-27 09:49

    The problem with those answers is that you're missing the key info. While this is a bit messy this is a quick version I came up with to make sure it contains the same info the MySQL Describe displays.

    Select SC.name AS 'Field', ISC.DATA_TYPE AS 'Type', ISC.CHARACTER_MAXIMUM_LENGTH AS 'Length', SC.IS_NULLABLE AS 'Null', I.is_primary_key AS 'Key', SC.is_identity AS 'Identity'
    From sys.columns AS SC 
    LEFT JOIN sys.index_columns AS IC
    ON IC.object_id = OBJECT_ID('dbo.Expenses') AND 
    IC.column_id = SC.column_id
    LEFT JOIN sys.indexes AS I 
    ON I.object_id = OBJECT_ID('dbo.Expenses') AND 
    IC.index_id = I.index_id
    LEFT JOIN information_schema.columns ISC
    ON ISC.TABLE_NAME = 'Expenses'
    AND ISC.COLUMN_NAME = SC.name
    WHERE SC.object_id = OBJECT_ID('dbo.Expenses')
    

提交回复
热议问题