我们正在尝试根据我们提供的值过滤一组 XML。
我们的数据库的 XML 字段中有以下 XML,如果通过数字“5052095050830”,我们需要在 XML 中找到这个特定的节点。我们提供的号码可能存在任意次数。
任何机构都可以提供一些示例 SQL 来协助吗?
谢谢
<Attributes>
<ProductVariantAttribute ID="4387">
<ProductVariantAttributeValue>
<Value>5052095050830</Value>
</ProductVariantAttributeValue>
</ProductVariantAttribute>
<ProductVariantAttribute ID="9999">
<ProductVariantAttributeValue>
<Value>5052095050830</Value>
</ProductVariantAttributeValue>
</ProductVariantAttribute>
<ProductVariantAttribute ID="4388">
<ProductVariantAttributeValue>
<Value>104401330A</Value>
</ProductVariantAttributeValue>
</ProductVariantAttribute>
<ProductVariantAttribute ID="4389">
<ProductVariantAttributeValue>
<Value>6905</Value>
</ProductVariantAttributeValue>
</ProductVariantAttribute>
<ProductVariantAttribute ID="4390">
<ProductVariantAttributeValue>
<Value>6906</Value>
</ProductVariantAttributeValue>
</ProductVariantAttribute>
<ProductVariantAttribute ID="4391">
<ProductVariantAttributeValue>
<Value>Monday, October 27, 2008</Value>
</ProductVariantAttributeValue>
</ProductVariantAttribute>
</Attributes>
最佳答案
您可以使用 .exist()
方法 - 像这样:
SELECT
(list of columns)
FROM
dbo.YourTable
WHERE
YourXmlColumn.exist('//Value[text()="5052095050830"]') = 1
这会检查您提供的特定值。您可以越精确地定义期望找到该值的 XPath,您的性能就越好。
YourXmlColumn.exist('//Value[text()="5052095050830"]') = 1
非常糟糕 - 它会检查每一个 <Value>
XML 中任意位置的节点以查找该值。
像这样:
YourXmlColumn.exist('/Attributes/ProductVariantAttribute/ProductVariantAttributeValue/Value[text()="5052095050830"]') = 1
会更加专注,因此性能会更好 - 但它只会那些由非常 XPath 语句定义的特定节点
关于xml - SQL 服务器 2008 XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5676271/