[英]Update XML node using XQuery
我有一個 XML 類型變量 @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>';
我想更新ID為 1 的節點的文本。
我試過這種方式
SET @tempXML = @xmlData
SELECT @xmlData;
SET @tempXML.modify('replace value of (/ArrayOfResult/Result/Text/text())[1] with ("This text is okay")');
SELECT @tempXML
但在這里,我不得不提到節點索引 [1] 來更新第一個節點。 如何更新ID = 1 的Text元素?
像這樣嘗試:
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;
--你可以使用一個變量來傳入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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.