[英]How can I find and update a node attribute value in an XML document based on match of a different attribute value in the same node?
Given my T-SQL stored procedure with: 鉴于我的T-SQL存储过程:
Declare @SearchID varchar(35)
Set @SearchID = '5280301.2019050148902.00023'
Declare @DocListXML XML
Contains the following XML data 包含以下XML数据
<JobList ListItems="7">
<Job JobFriendlyName="EMAIL INVOICES">
<DocumentList>
<Document Doc="1" ID="5280301.2019050148902.00020" Date="05-03-2019" Status="NEW" />
<Document Doc="2" ID="5280301.2019050148902.00022" Date="05-03-2019" Status="NEW" />
<Document Doc="3" ID="5280301.2019050148902.00023" Date="05-03-2019" Status="NEW" />
<Document Doc="4" ID="5280301.2019050104301.00055" Date="05-02-2019" Status="NEW" />
<Document Doc="5" ID="5280301.2019050104301.00056" Date="05-02-2019" Status="NEW" />
</DocumentList>
</Job>
<Job JobFriendlyName="INVOICES">
<DocumentList>
<Document Doc="6" ID="5280300.2019050148901.00001" Date="05-03-2019" Status="NEW" />
<Document Doc="7" ID="5280300.2019050148901.00002" Date="05-03-2019" Status="NEW" />
</DocumentList>
</Job>
</JobList>
I need T-SQL XML code to update this document and replace existing Status
value with "OLD" for the node having ID
attribute value matching the value of T-SQL local variable @SearchID
. 我需要T-SQL XML代码来更新此文档,并将具有
ID
属性值与T-SQL本地变量@SearchID
值匹配的节点的现有Status
值替换为“OLD”。
After the update, the resulting document in @DocListXML
should contain: 更新后,
@DocListXML
的结果文档应包含:
<JobList ListItems="7">
<Job JobFriendlyName="EMAIL INVOICES">
<DocumentList>
<Document Doc="1" ID="5280301.2019050148902.00020" Date="05-03-2019" Status="NEW" />
<Document Doc="2" ID="5280301.2019050148902.00022" Date="05-03-2019" Status="NEW" />
<Document Doc="3" ID="5280301.2019050148902.00023" Date="05-03-2019" Status="OLD" />
<Document Doc="4" ID="5280301.2019050104301.00055" Date="05-02-2019" Status="NEW" />
<Document Doc="5" ID="5280301.2019050104301.00056" Date="05-02-2019" Status="NEW" />
</DocumentList>
</Job>
<Job JobFriendlyName="INVOICES">
<DocumentList>
<Document Doc="6" ID="5280300.2019050148901.00001" Date="05-03-2019" Status="NEW" />
<Document Doc="7" ID="5280300.2019050148901.00002" Date="05-03-2019" Status="NEW" />
</DocumentList>
</Job>
</JobList>
I have found the modify/replace code to modify an attribute value of "X" "Y", but cannot find code for modify/update updating attribute [a] value in node with attribute [b] value matching a local variable. 我找到修改/替换代码来修改属性值“X”“Y”,但找不到修改/更新更新属性[a]值的代码,其中属性[b]值与局部变量匹配。
Any help or suggestions would be appreciated. 任何帮助或建议将不胜感激。
Okay, after your last question you decided to got the CURSOR
route, correct? 好的,在您上一个问题之后,您决定使用
CURSOR
路线,对吗? ;-) ;-)
Try it like this. 试试吧。
--Your mockup up
Declare @SearchID varchar(35) = '5280301.2019050148902.00023';
Declare @DocListXML XML=
N'<JobList ListItems="7">
<Job JobFriendlyName="EMAIL INVOICES">
<DocumentList>
<Document Doc="1" ID="5280301.2019050148902.00020" Date="05-03-2019" Status="NEW" />
<Document Doc="2" ID="5280301.2019050148902.00022" Date="05-03-2019" Status="NEW" />
<Document Doc="3" ID="5280301.2019050148902.00023" Date="05-03-2019" Status="NEW" />
<Document Doc="4" ID="5280301.2019050104301.00055" Date="05-02-2019" Status="NEW" />
<Document Doc="5" ID="5280301.2019050104301.00056" Date="05-02-2019" Status="NEW" />
</DocumentList>
</Job>
<Job JobFriendlyName="INVOICES">
<DocumentList>
<Document Doc="6" ID="5280300.2019050148901.00001" Date="05-03-2019" Status="NEW" />
<Document Doc="7" ID="5280300.2019050148901.00002" Date="05-03-2019" Status="NEW" />
</DocumentList>
</Job>
</JobList>';
--This is the modifying command - 这是修改命令
SET @DocListXML.modify(N'replace value of (/JobList
/Job
/DocumentList
/Document[@ID=sql:variable("@SearchID")]
/@Status)[1] with "OLD"');
--check the result - 检查结果
SELECT @DocListXML;
Hint 1: This works only in cases where there is just one document with the given ID per XML! 提示1:这仅适用于每个XML只有一个具有给定ID的文档的情况!
Hint 2: You might shorten the command to this: 提示2:您可以将命令缩短为:
SET @DocListXML.modify(N'replace value of (//Document[@ID=sql:variable("@SearchID")]/@Status)[1] with "OLD"');
The deep search (triggered by the double slash //
at the beginning of the XPath) tells the engine to find a <Document>
fullfilling the predicate anywhere in your XML. 深度搜索 (由XPath开头的双斜杠
//
触发)告诉引擎在XML中的任何位置找到一个<Document>
填充谓词 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.