【问题标题】:SQL Server XQuery Syntax ErrorSQL Server XQuery 语法错误
【发布时间】:2018-03-02 07:35:30
【问题描述】:

我正在为 SQLServer 使用 Serilog 接收器,并使用 this 链接遵循 DB 架构和 XQuery 语法。然而,当我在 Navicat 中运行以下查询时,我得到了错误。

这是查询:

SELECT 
Properties.value('(/properties/property[@key="contact"]/structure[@type="Contact"]/property[@key="ContactId"])[1]', 'nvarchar(max)') AS ContactId,
Properties.value('(/properties/property[@key="contact"]/structure[@type="Contact"]/property[@key="FirstName"])[1]', 'nvarchar(50)') AS FirstName,
Properties.value('(/properties/property[@key="contact"]/structure[@type="Contact"]/property[@key="Surname"])[1]', 'nvarchar(100)') AS Surname,
Properties.value('(/properties/property[@key="cacheKey"])[1]', 'nvarchar(100)') AS CacheKey,
*
FROM Log
WHERE MessageTemplate = 'Contact {@contact} added to cache with key {@cacheKey}'
    AND Properties.value('(/properties/property[@key="contact"]/structure[@type="Contact"]/property[@key="ContactId"])[1]', 'nvarchar(max)') = 'f7d10f53-4c11-44f4-8dce-d0e0e22cb6ab' 

这是错误声明:

[Err] 42000 - [SQL Server]找不到列“Properties”或用户定义的函数或聚合“Properties.value”,或者名称不明确。

任何解决方案或解决方法都将受到高度赞赏。

【问题讨论】:

  • Properties是XML格式吗?如果Log 表有Properties 列并且其格式为XML,则您的查询将运行。
  • 不,不是……

标签: sql sql-server tsql xquery serilog


【解决方案1】:

如果 Log 表有 Properties 列并且它的格式是 XML,您的查询就会运行。

因此,您必须在查询中更改列的类型或将其转换为 XML。

第二种方法看起来像cast(Properties as xml).value...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 2015-12-10
    • 1970-01-01
    相关资源
    最近更新 更多