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

前端 未结 23 933
一整个雨季
一整个雨季 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:51

    Just double click on the table name and press Alt+F1

    0 讨论(0)
  • 2020-11-27 09:53
    CREATE PROCEDURE [dbo].[describe] 
    ( 
    @SearchStr nvarchar(max) 
    ) 
    AS 
    BEGIN 
    SELECT  
        CONCAT([COLUMN_NAME],' ',[DATA_TYPE],' ',[CHARACTER_MAXIMUM_LENGTH],' ', 
        (SELECT CASE [IS_NULLABLE] WHEN 'NO' THEN 'NOT NULL' ELSE 'NULL' END),
        (SELECT CASE WHEN [COLUMN_DEFAULT] IS NULL THEN '' ELSE CONCAT(' DEFAULT ',[COLUMN_DEFAULT]) END)
        ) AS DESCRIPTION
        FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME LIKE @SearchStr
    END 
    
    0 讨论(0)
  • 2020-11-27 09:55
    SELECT C.COLUMN_NAME, C.IS_NULLABLE, C.DATA_TYPE, TC.CONSTRAINT_TYPE, C.COLUMN_DEFAULT
        FROM INFORMATION_SCHEMA.COLUMNS AS C
        FULL JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CC ON C.COLUMN_NAME = CC.COLUMN_NAME 
        FULL JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC ON CC.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
    WHERE C.TABLE_NAME = '<Table Name>';
    

    Sample Output

    0 讨论(0)
  • 2020-11-27 09:55

    select * from sysobjects where name='TABLENAME'

    0 讨论(0)
  • 2020-11-27 09:56

    Please use the following sql query; this worked for my case.

    select * FROM   INFORMATION_SCHEMA.Columns where table_name = 'tablename';
    
    0 讨论(0)
  • 2020-11-27 09:59

    I like this format:

    name     DataType      Collation             Constraints         PK  FK          Comment
    
    id       int                                 NOT NULL IDENTITY   PK              Order Line Id
    pid      int                                 NOT NULL                tbl_orders  Order Id
    itemCode varchar(10)   Latin1_General_CI_AS  NOT NULL                            Product Code
    

    So I have used this:

    DECLARE @tname varchar(100) = 'yourTableName';
    
    SELECT  col.name,
    
            CASE typ.name
                WHEN 'nvarchar' THEN 'nvarchar('+CAST((col.max_length / 2) as varchar)+')'
                WHEN 'varchar' THEN 'varchar('+CAST(col.max_length as varchar)+')'
                WHEN 'char' THEN 'char('+CAST(col.max_length as varchar)+')'
                WHEN 'nchar' THEN 'nchar('+CAST((col.max_length / 2) as varchar)+')'
                WHEN 'binary' THEN 'binary('+CAST(col.max_length as varchar)+')'
                WHEN 'varbinary' THEN 'varbinary('+CAST(col.max_length as varchar)+')'
                WHEN 'numeric' THEN 'numeric('+CAST(col.precision as varchar)+(CASE WHEN col.scale = 0 THEN '' ELSE ','+CAST(col.scale as varchar) END) +')'
                WHEN 'decimal' THEN 'decimal('+CAST(col.precision as varchar)+(CASE WHEN col.scale = 0 THEN '' ELSE ','+CAST(col.scale as varchar) END) +')'
                ELSE typ.name
                END DataType,
    
            ISNULL(col.collation_name,'') Collation,
    
            CASE WHEN col.is_nullable = 0 THEN 'NOT NULL ' ELSE '' END + CASE WHEN col.is_identity = 1 THEN 'IDENTITY' ELSE '' END Constraints,
    
            ISNULL((SELECT 'PK'
                    FROM    sys.key_constraints kc INNER JOIN
                            sys.tables tb ON tb.object_id = kc.parent_object_id INNER JOIN
                            sys.indexes si ON si.name = kc.name INNER JOIN
                            sys.index_columns sic ON sic.index_id = si.index_id AND sic.object_id = si.object_id
                    WHERE kc.type = 'PK'
                      AND tb.name = @tname
                      AND sic.column_id = col.column_id),'') PK,
    
            ISNULL((SELECT (SELECT name FROM sys.tables st WHERE st.object_id = fkc.referenced_object_id)
                    FROM    sys.foreign_key_columns fkc INNER JOIN
                            sys.columns c ON c.column_id = fkc.parent_column_id AND fkc.parent_object_id = c.object_id INNER JOIN
                            sys.tables t ON t.object_id = c.object_id
                    WHERE t.name = tab.name
                      AND c.name = col.name),'') FK,
    
            ISNULL((SELECT value
                    FROM sys.extended_properties
                    WHERE major_id = tab.object_id
                      AND minor_id = col.column_id),'') Comment
    
    FROM sys.columns col INNER JOIN
         sys.tables tab ON tab.object_id = col.object_id INNER JOIN
         sys.types typ ON typ.system_type_id = col.system_type_id
    WHERE tab.name = @tname
      AND typ.name != 'sysname'
    ORDER BY col.column_id;
    
    0 讨论(0)
提交回复
热议问题