3

I have a column name XMLData which contain large XML (around 10,000 lines). Below is the type of XML stored.

<ABC>
  <DEF>
    <GHI>
      <JKL>value1</JKL>
      ..
    </GHI>
    <GHI>
      <JKL>value2</JKL>
      ..
    </GHI>
    <GHI>
      <JKL>value3</JKL>
      ..
    </GHI>
    ..
    ..
    <GHI>
      <JKL>valueN</JKL>
      ..
    </GHI>
    <OtherNodes>
    <OtherNodes1>
    .
    .
    .
  </DEF>
</ABC>

Is there a way to delete multiple nodes in SQL query? Specifically, I want to delete all GHI nodes under the Node DEF. Thanks!!

3
  • It probably more efficient to export, row, modify, xml, the update row. If you are using SQL Server I recommend on of the command line executable like sqlcmd.exe or bcp.exe. See msdn.microsoft.com/en-us/library/ms162816.aspx Commented Jul 22, 2015 at 10:29
  • @jdweng - do you have a specific reason for avoiding the SQL Server xml data type functionality (I'm always happy to learn something new) Commented Jul 22, 2015 at 10:40
  • It is easier to parse xml outside sql than inside. Outside you can use other languages not avaiable inside. You can put the call to the command language inside other programs using SHELL for example. Commented Jul 22, 2015 at 11:35

1 Answer 1

2

For SQL Server, you'd just use modify with delete:

declare @x xml = '<ABC>
  <DEF>
    <GHI>
      <JKL>value1</JKL>
    </GHI>
    <GHI>
      <JKL>value2</JKL>
    </GHI>
    <GHI>
      <JKL>value3</JKL>
    </GHI>
    <GHI>
      <JKL>valueN</JKL>
    </GHI>
    <OtherNodes/>
    <OtherNodes1/>
  </DEF>
</ABC>'

set @x.modify('delete /ABC/DEF/GHI')

select @x

Result:

<ABC><DEF><OtherNodes /><OtherNodes1 /></DEF></ABC>
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.