You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SQL中查询XML字段的特定节点?

Fixing Your XML Query to Get Specific Nodes

The issue with your query is that you’re treating elem as a child element instead of an attribute of the prop node. In your XML, elem="_NoFilter" is an attribute, not a nested element—so you need to use the @ prefix in your XPath to target attributes correctly.

Additionally, since the _NoFilter prop has a specific namespace identifier (ns="ARM:"), it’s a good idea to include that in your predicate to ensure you’re matching the exact node you want (in case there are other props with the same elem value but different ns values).

Corrected Query to Retrieve the Full Prop Node

;WITH XMLNAMESPACES(DEFAULT 'http://www.xyz.de/activerepository/fileprops')
SELECT 
    CAST(properties AS XML).query('root/props/prop[@ns="ARM:" and @elem="_NoFilter"]'),
    *
FROM tm_cas_files (NOLOCK)
WHERE id = 'A7ED7D99-8CDB-47F6-8EF5-18E7FCB97F28'

If You Only Need the Value Inside the Node

If you don’t need the entire prop node and just want the actual value (like the boolean true in this case), use the value() method instead of query():

;WITH XMLNAMESPACES(DEFAULT 'http://www.xyz.de/activerepository/fileprops')
SELECT 
    CAST(properties AS XML).value('(root/props/prop[@ns="ARM:" and @elem="_NoFilter"]/value)[1]', 'BIT') AS NoFilterValue,
    *
FROM tm_cas_files (NOLOCK)
WHERE id = 'A7ED7D99-8CDB-47F6-8EF5-18E7FCB97F28'

Why Your Original Query Returned Empty Results

Your XPath root/props/prop[elem="_NoFilter"] was looking for prop nodes that have a child element named elem with the value "_NoFilter". But in your XML, elem is an attribute, not a child element—so that predicate never matched any nodes, resulting in an empty output.

内容的提问来源于stack exchange,提问作者dns_nx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:02:13