I have a XML type variable @XMLData.
DECLARE @xmlData XML
DECLARE @tempXML XML
SET @xmlData =N'<ArrayOfResult>
<Result>
<ID>1</ID>
<Text>This text should be updated to new text</Text>
</Result>
<Result>
<ID>2</ID>
<Text>This text is okay</Text>
</Result>
</ArrayOfResult>';
I want to update the of the nodes where is 1.为 1 的节点的。
I have tried this way
SET @tempXML = @xmlData
SELECT @xmlData;
SET @tempXML.modify('replace value of (/ArrayOfResult/Result/Text/text())[1] with ("This text is okay")');
SELECT @tempXML
But here, I have to mention the node index [1] to update first node. How can I update the element which have = 1 ? = 1 的元素?
Try it like this:
DECLARE @xmlData XML
DECLARE @tempXML XML
SET @xmlData =N'<ArrayOfResult>
<Result>
<ID>1</ID>
<Text>This text should be updated to new text</Text>
</Result>
<Result>
<ID>2</ID>
<Text>This text is okay</Text>
</Result>
</ArrayOfResult>';
SET @tempXML = @xmlData
SELECT @xmlData;
--you can use a variable to pass in the id
DECLARE @id INT=1;
SET @tempXML.modify('replace value of (/ArrayOfResult/Result[ID=sql:variable("@id")]/Text/text())[1] with ("This text is okay")');
SELECT @tempXML
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.