Sql server 使用XQuery更新XML节点
我有一个XML类型变量@XMLDataSql 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&
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