如何从SQL中的XML变量删除包含多个属性的节点

时间:2019-03-22 17:57:15

标签: sql sql-server xml xquery

我有一些XML传递到如下所示的SQL存储过程中:

<root>
    <category>
        <property id="blah" otherID="blahblah" year="2015">
        <property id="blah" otherID="notBlahBlah" year="2015">
    </category>
</root>

我想使用基于多个节点属性的SQL从中删除属性节点。例如,假设我要删除id =“ blah”和otherID =“ blahblah”的属性。我该怎么办?谢谢。

到目前为止,这是我在阅读一些XQUERY文档后对如何执行此操作的最佳猜测而得出的代码,但是它无法正常工作:

DECLARE @XML AS XML
SET @XML = '<root>
               <category>
                   <property id="blah" otherID="blahblah" year="2015">
                   <property id="blah" otherID="notBlahBlah" year="2015">
               </category>
            </root>'

UPDATE @XML.nodes('/root/category/property') 
SET data.modify('delete /root/category/property[id="blah", otherID="blahblah"]')    

1 个答案:

答案 0 :(得分:1)

DECLARE @XML AS XML

SET @XML = '
<root>
               <category>
                   <property id="blah" otherID="blahblah" year="2015"/>
                   <property id="blah" otherID="notblahblah" year="2015"/>
               </category>
            </root>'

SET @XML.modify('delete /root/category/property[@id="blah" and @otherID="blahblah"]')

SELECT @XML