列出表和表扩展属性

Posted

技术标签:

【中文标题】列出表和表扩展属性【英文标题】:List Tables and Table Extended Properties 【发布时间】:2014-02-11 03:59:42 【问题描述】:

我想返回我的数据库中所有表的列表以及该表的任何相关扩展属性。

这给了我表格列表:

    SELECT TABLE_NAME AS tTableName, TABLE_TYPE AS tTableType
    FROM information_schema.tables AS t
    WHERE TABLE_TYPE = N'BASE TABLE'
    ORDER BY TABLE_NAME

这为我提供了包含它们的表的扩展属性列表:

    SELECT OBJECT_NAME(ep.major_id) AS [epTableName],
    CAST(ep.Value AS nvarchar(500)) AS [epExtendedProperty]
    FROM sys.extended_properties AS ep
    WHERE ep.name = N'MS_Description' AND ep.minor_id = 0

我看不到如何组合这些并正确保留 WHERE 条件。这是错误的,但它会让您了解我哪里出错了:

    SELECT TABLE_NAME AS tTableName, TABLE_TYPE AS tTableType,
    OBJECT_NAME(ep.major_id) AS [epTableName],
    CAST(ep.Value AS nvarchar(500)) AS [epExtendedProperty]
    FROM information_schema.tables AS t
    LEFT OUTER JOIN sys.extended_properties AS ep ON
    t.table_name= OBJECT_NAME(ep.major_id) 
    WHERE TABLE_TYPE = N'BASE TABLE'
    -- No clue where to put this: AND ep.name = N'MS_Description' AND ep.minor_id = 0
    ORDER BY TABLE_NAME

从 Simon1979 的回答下方乔的评论中添加...

SELECT 
    t.Table_Name, 
    q.epExtendedProperty 
FROM 
    information_schema.tables t 
    LEFT OUTER JOIN 
   (SELECT 
        OBJECT_NAME(ep.major_id) epTableName, 
        CAST(ep.Value AS nvarchar(500)) AS epExtendedProperty 
    FROM 
        sys.extended_properties ep 
    WHERE 
        ep.name = N'MS_Description' AND 
        ep.minor_id = 0) q 
        ON  t.Table_Name = q.epTableName 
WHERE 
    t.Table_Type = N'BASE TABLE' AND 
    t.Table_Name <> 'sysdiagrams' 
ORDER BY 
    q.epExtendedProperty ASC, 
    t.Table_Name ASC 

【问题讨论】:

【参考方案1】:

我认为以下实现了您想要的,尽管我的表上没有任何符合标准的扩展属性,因此很难检查它是否有效:

SELECT TABLE_NAME AS tTableName, 
    TABLE_TYPE AS tTableType,
    q.epTableName,
    q.epExtendedProperty
FROM information_schema.tables AS t
    LEFT OUTER JOIN (SELECT OBJECT_NAME(ep.major_id) AS [epTableName],
        CAST(ep.Value AS nvarchar(500)) AS [epExtendedProperty]
        FROM sys.extended_properties ep
        WHERE ep.name = N'MS_Description' AND ep.minor_id = 0) As q
    ON t.table_name = q.epTableName 
WHERE TABLE_TYPE = N'BASE TABLE'
ORDER BY TABLE_NAME

【讨论】:

我将原始语句清理为下面的已完成语句,该语句按我的意愿工作:'SELECT t.Table_Name, q.epExtendedProperty FROM information_schema.tables t LEFT OUTER JOIN (SELECT OBJECT_NAME(ep .major_id) epTableName, CAST(ep.Value AS nvarchar(500)) AS epExtendedProperty FROM sys.extended_properties ep WHERE ep.name = N'MS_Description' AND ep.minor_id = 0) q ON t.Table_Name = q.epTableName WHERE t .Table_Type = N'BASE TABLE' AND t.Table_Name 'sysdiagrams' ORDER BY q.epExtendedProperty ASC, t.Table_Name ASC'

以上是关于列出表和表扩展属性的主要内容,如果未能解决你的问题,请参考以下文章

从 C# 检索 SQL Server 扩展属性

使用 openssl 从自签名证书生成的证书签名请求是不是应该显示扩展属性?

@table注解详解

如何扩展一个sql表?

MyBatis表和实体关联

数据库扩展性设计:使用二进制解决一条记录关联多个状态的问题