How to describe table in SQL Server 2008?

前端 未结 8 1437
长情又很酷
长情又很酷 2020-12-24 05:33

I want to describe a table in SQL Server 2008 like what we can do with the DESC command in Oracle.

I have table [EX].[dbo].[EMP_MAST] which

相关标签:
8条回答
  • 2020-12-24 05:52

    Just enter the below line.

    exec sp_help [table_name]
    
    0 讨论(0)
  • 2020-12-24 05:55

    As a variation of Bridge's answer (I don't yet have enough rep to comment, and didn't feel right about editing that answer), here is a version that works better for me.

    SELECT column_name AS [Name],
       IS_NULLABLE AS [Null?],
       DATA_TYPE + CASE
                     WHEN CHARACTER_MAXIMUM_LENGTH IS NULL THEN ''
                     WHEN CHARACTER_MAXIMUM_LENGTH > 99999 THEN ''
                     ELSE '(' + Cast(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(5)) + ')' 
                   END AS [Type]
    FROM   INFORMATION_SCHEMA.Columns
    WHERE  table_name = 'table_name'
    

    Notable changes:

    • Works for types without length. For an int column, I was seeing NULL for the type because the length was null and it wiped out the whole Type column. So don't print any length component (or parens).
    • Change the check for CAST length of -1 to check actual length. I was getting a syntax error because the case resulted in '*' rather than -1. Seems to make more sense to perform an arithmetic check rather than an overflow from the CAST.
    • Don't print length when very long (arbitrarily > 5 digits).
    0 讨论(0)
提交回复
热议问题