Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用XQuery更新XML节点_Sql Server_Xml_Xquery - Fatal编程技术网

Sql server 使用XQuery更新XML节点

Sql server 使用XQuery更新XML节点,sql-server,xml,xquery,Sql Server,Xml,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&

我有一个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>';
但在这里,我必须提到更新第一个节点的节点索引[1]。 如何更新ID为1的文本元素

像这样试试:

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;

@NoorAShuvo,只有一个提示:您也可以通过变量引入要替换的文本。如果该值取自结果集的列,请使用
sql:column(“ColumnName”)
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;
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