简体   繁体   中英

How to delete nodes containing multiple attributes from XML variable in SQL

I have some XML being passed into a SQL stored procedure that looks like this:

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

I want to delete a property node from this using SQL based on multiple node attributes. For example, say I wanted to delete a property with id="blah" and otherID="blahblah". How could I do this? Thank you.

This is the code I have so far based on my best guess of how to do this having read some XQUERY documentation, but it's not working correctly:

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"]')    
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM