简体   繁体   English

尝试使用XQuery修改SQL Server中的XML

[英]Attempting To Modify XML in SQL Server Using XQuery

I am attempting to add an attribute to a node in an XML column in SQL Server. 我正在尝试向SQL Server中的XML列中的节点添加属性。

UPDATE
    TableName
SET Metadata.modify('
    insert attribute MyAttribute{"01b9cd0b-bfed-436f-bc58-57d2fddd9211"}
    into (Root/Collection/Item[@No="360"][1])
')
WHERE
    TableName.Id = 1

I get the following error... 我收到以下错误...

Msg 2226, Level 16, State 1, Line 4 XQuery Msg 2226,Level 16,State 1,Line 4 XQuery
[TableName.Metadata.modify()]: The target of 'insert' must be a single node, found 'element(Item,xdt:untyped) *' [TableName.Metadata.modify()]:'insert'的目标必须是单个节点,找到'element(Item,xdt:untyped)*'

But I thought my selection would return a single item, given the [1] 但我认为我的选择将返回一个项目,给出[1]

Stupid XQuery! 愚蠢的XQuery! (Or possibly me). (或者可能是我)。

You need to place the [1] outside of the brackets: 您需要将[1]放在括号之外:

into (Root/Collection/Item[@No="360"][1])

Should be 应该

into (Root/Collection/Item[@No="360"])[1]

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

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