Skip to content

SqlServer 如何获取表和表结构?

获取所有非系统表

sql
SELECT name FROM sys.databases WHERE database_id > 4

获取表名及表备注

sql
SELECT
        a.name AS Name,
        ISNULL(g.[value],'-') AS Description
FROM
        sys.tables a 
        LEFT JOIN sys.extended_properties g
        ON      (a.object_id           = g.major_id 
                        AND g.minor_id = 0 
                )

获取表的所有列名 | 备注 | 类型

sql
SELECT
        a.name AS Name,
        ISNULL(g.[value],'-') AS Description,
        c.name AS Type
FROM
        sys.columns a
        LEFT JOIN sys.extended_properties g
        ON      (a.object_id           = g.major_id 
                        AND g.minor_id = a.column_id 
                )
        LEFT JOIN systypes c
        ON      a.system_type_id = c.xusertype
WHERE   a.object_id              =
        (SELECT object_id FROM sys.tables WHERE name = @TableName 
        )