如何解决SQL Server 2008 XPath
| 我们正在尝试根据提供的值来过滤一组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
非常糟糕-它会查询XML中任何位置的每个“ 4”节点,以找到该值。
像这样:
YourXmlColumn.exist(\'/Attributes/ProductVariantAttribute/ProductVariantAttributeValue/Value[text()=\"5052095050830\"]\') = 1
会更加集中,因此性能会更好-但是只会使用XPath语句定义的那些特定节点
,这将值作为另一个XML返回,我不确定您的需要如何。
DECLARE @xml AS XML
SET @xml =
\'<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>\'
SELECT @xml.query
(\'
for $text in //Attributes/ProductVariantAttribute
where $text/ProductVariantAttributeValue/Value[text()] = 5052095050830
return string($text/@ID)
\')
,如果要在此处检索属性@ID,则为:
DECLARE @xml AS XML
SET @xml =
\'<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,2008</Value>
</ProductVariantAttributeValue>
</ProductVariantAttribute>
</Attributes>\'
select
t.x.value(\'@ID\',\'varchar(50)\') ProductVariantAttributeID
from @xml.nodes(\'//ProductVariantAttribute\') t(x)
where t.x.value(\'(ProductVariantAttributeValue/Value)[1]\',\'nvarchar(50)\') = \'5052095050830\'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。