简体   繁体   English

更新SQL Server中的xml节点属性,过滤其他属性

[英]Update xml node attributes in SQL Server, filtering on other attribute

I'm trying to update node attributes in an XML column and I'm stuck. 我正在尝试更新XML列中的节点属性,但被卡住了。 Admittedly, XQuery was white noise for me until a day ago, and I could persevere some more, but that would hurt my customer. 可以肯定的是,直到一天前, XQuery还是对我来说是白噪声,我可以继续坚持下去,但这会伤害我的客户。 I've trawled SO up and down, still just as stuck. 我拖网左右上下,还是一样卡住。

So here are the pertinent parts of my setup: 因此,这是我设置的相关部分:

Table: 表:

CREATE TABLE Tbl(Id int IDENTITY, XmlCol XML)
INSERT Tbl VALUES(
'<root>
  <item ID="1">some text input</item>
  <item ID="7" PROP="10">1</item>
</root>
'
)
INSERT Tbl VALUES(
'<root>
  <item ID="1">some other text input</item>
  <item ID="8" PROP="10">1</item>
</root>
'
)

Context? 语境? Don't ask me why, it's so contrived, you'd get stuck trying to make sense of it. 不要问我为什么,它是如此人为设计,您在尝试理解它时会陷入困境。

For all item nodes with an ID attribute of "8", I need to increase the PROP attribute with 30. When that's done, I need to update the ID attribute from "8" to "7" for matching nodes, effectively merging the items, while keeping their PROP values apart. 对于ID属性为“ 8”的所有项目节点,我需要将PROP属性增加为30。完成后,我需要将ID属性从“ 8”更新为“ 7”以匹配节点,从而有效地合并项目,同时保持PROP值分开。

Here's as close as I've got, but SQL Server (or me) is not playing nice. 这距离我已经很近了,但是SQL Server(或我)玩的不好。

Update statement: 更新语句:

DECLARE @newVal INT = 40

update Tbl
set XmlCol.modify('replace value of (/root/item[@ID="8"][@PROP="10"]/@PROP)[1]
                   with sql:variable("@newVal") ')

Result: 结果:

XQuery [Tbl.XMLCol.modify()]: ")" was expected. XQuery [Tbl.XMLCol.modify()]:“)”。

Anyone? 任何人?

You just have to use and inside the [] , like this: 您只需要在[]使用and ,如下所示:

update Tbl set
   XmlCol.modify('
      replace value of (/root/item[@ID="8" and @PROP="10"]/@PROP)[1]
      with sql:variable("@newVal")
   ')

sql fiddle demo sql小提琴演示

I was experiencing a similar issue like @nkstr. 我遇到类似@nkstr的类似问题。 And the answer from @Roman Pekar helped me get a good idea on how to update attribute values, filtering on other attribute. @Roman Pekar的回答使我对如何更新属性值,过滤其他属性有了一个好主意。 Here is the SQL I used. 这是我使用的SQL。

declare @X xml = 
'<root>
<items>
    <item ItemID="100">
        <obj ObjID="0001" value="val1"/>
        <obj ObjID="0002" value="val2"/>
        <obj ObjID="0003" value="val3"/>
        <obj ObjID="0004" value="val4"/>
    </item>
    <item ItemID="200">
        <obj ObjID="0001" value="val1"/>
        <obj ObjID="0002" value="val2"/>
        <obj ObjID="0003" value="val3"/>
        <obj ObjID="0004" value="val4"/>
    </item>
</items>
</root>'

declare @ITEMID int = 200 
declare @OBJID int = 0004
declare @NEWVAL int = 8888

--update ObjID 0004 at ItemID 200 into 8888 (this is a comment)

set @X.modify('replace value of  (/root/items/item[@ItemID=sql:variable("@ITEMID")]/obj[@ObjID=sql:variable("@OBJID")]/@ObjID)[1] with sql:variable("@NEWVAL")')

select @X

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM