是否可以在 SQL Server 中查询空扩展属性?

Posted

技术标签:

【中文标题】是否可以在 SQL Server 中查询空扩展属性?【英文标题】:Is it possible to query a null extended property in SQL Server? 【发布时间】:2021-03-03 17:47:57 【问题描述】:

我正在尝试想出一种方法来查询一个结果集,该结果集为没有关联扩展属性的任何列返回架构/表/列,以便我的团队可以了解在哪里添加文档。

话虽如此...是否可以查询没有值的扩展属性?我尝试了以下方法,并用p.value = '' 切换了最后一行,但没有雪茄。

SELECT
    SCHEMA_NAME(tbl.schema_id) AS SchemaName, 
    tbl.name AS TableName, 
    clmns.name AS ColumnName
FROM 
    sys.tables AS tbl
INNER JOIN sys.all_columns AS clmns ON clmns.object_id=tbl.object_id
INNER JOIN sys.extended_properties AS p ON p.major_id=tbl.object_id 
WHERE SCHEMA_NAME(tbl.schema_id) = 'schema_name'
   AND tbl.name = 'table_name'
   AND p.value is null

【问题讨论】:

WHERE NOT EXISTS (SELECT 1 FROM sys.extended_properties ... 有效吗? “没有价值”与“不存在”不同。您的查询将适用于值为 NULL 的扩展属性,这与不存在的属性不同。您的标题似乎要求一件事,而问题主体则是另一件事。 【参考方案1】:

试试这个:

SELECT
    s.[name] AS [schema_name]
    , t.[name] AS [table_name]
    , c.[name] AS [column_name]
    , x.[value] AS [MS_Description]
FROM [sys].[schemas] AS s
INNER JOIN [sys].[tables] AS t
    ON s.[schema_id] = t.[schema_id]
INNER JOIN [sys].[columns] AS c
    ON t.[object_id] = c.[object_id]
LEFT OUTER JOIN [sys].[extended_properties] AS x
    ON t.[object_id] = x.[major_id]
    AND c.[column_id] = x.[minor_id]
    AND x.[name] = 'MS_Description'
WHERE
    t.[name] = 'your_table_name'
    AND s.[name] = 'your_schema_name';

示例结果集:

+-------------+------------+----------------+-------------------------+
| schema_name | table_name |  column_name   |     MS_Description      |
+-------------+------------+----------------+-------------------------+
| dbo         | tblClients | pk_ClientID    | NULL                    |
| dbo         | tblClients | ClientName     | Client's business name. |
| dbo         | tblClients | PrimaryContact | NULL                    |
| dbo         | tblClients | Addr           | NULL                    |
| dbo         | tblClients | Addr2          | NULL                    |
| dbo         | tblClients | Addr3          | NULL                    |
| dbo         | tblClients | City           | NULL                    |
| dbo         | tblClients | State          | NULL                    |
| dbo         | tblClients | Zipcode        | NULL                    |
| dbo         | tblClients | Phone          | NULL                    |
+-------------+------------+----------------+-------------------------+

【讨论】:

以上是关于是否可以在 SQL Server 中查询空扩展属性?的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server中常用的SQL语句

SQL Server 分析服务在维度字符串属性中传递空值和空白值

sql server sql语言 default

如何判断SQL SERVER表中字段为空

使用SSIS从SQL Server 2005中的平面文件导入时如何保留NULL值

是否可以在 SQL Server 中同时运行 2 个查询