簡體   English   中英

使用 XQuery 更新 XML 節點

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM