C# 如何在SQLServer中查询XML列中的数据
我在C# 如何在SQLServer中查询XML列中的数据,c#,sql,sql-server,xml,C#,Sql,Sql Server,Xml,我在xml列中有这样的数据: <product> <productID>1</productID> <productname>tea</productname> </product> <product> <productID>2</productID> <productname>coffee</productname> </produc
xml
列中有这样的数据:
<product>
<productID>1</productID>
<productname>tea</productname>
</product>
<product>
<productID>2</productID>
<productname>coffee</productname>
</product>
但在这里,它总是会改变第二个产品的价值。请告诉我如何使用
productID
进行查询首先,XML
是区分大小写的,XQuery
表达式也是区分大小写的
现在你可以这么做了
UPDATE [dbo].ProductDocs
SET ProductDoc.modify('replace value of (/product[productID=2]/productname/text())[1] with "GreenTea"')
使用谓词表达式按
productID
值过滤product
元素,如下所示:
UPDATE [dbo].ProductDocs
SET ProductDoc.modify('
replace value of (/product[productID=2]/productname/text())[1] with "NewName"
')
另外请注意,正如另一个答案中提到的,XML和XPath/XQuery区分大小写。同意区分大小写。但是XQuery将触发“语法错误”,因为
/[some_expression]
不是有效的表达式。此外,productID/@Value
引用了productID
元素的Value
属性,OP的XML中没有这样的属性。@har07是的。。我认为这是一个价值观,现在修正了。
UPDATE [dbo].ProductDocs
SET ProductDoc.modify('
replace value of (/product[productID=2]/productname/text())[1] with "NewName"
')