0

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"]')    
2
  • Please provide sample outcome and what you have tried so far. Here is a link for what is considered necessary for a good SQL question Commented Mar 22, 2019 at 17:59
  • XML support is highly vendor-specific - so please add a tag to specify whether you're using mysql, postgresql, sql-server, oracle or db2 - or something else entirely. Commented Mar 22, 2019 at 19:13

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.