user3482471
user3482471

Reputation: 227

How to delete node from XML column in DB

I am trying to remove a particular "node" from an XML column in my SQL Table. Below is an example of one of the XML column contents.

<GodBrandConfig>
  <AppSecret>hello</AppSecret>
  <WebClientUrl>url</WebClientUrl>
  <AllowableIpAddresses>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>.*</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>178.160.245.88</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>178.160.245.88</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
  </AllowableIpAddresses>
  <GameplaySummaryUrl>about:blank</GameplaySummaryUrl>
</GodBrandConfig>

I am trying to delete duplicate records in here - For example " 178.160.245.88"

I have been trying many variations of the "delete" statement - Please can i have some assistance on this. set column.modify('delete /GodBrandConfig/AllowableIpAddresses/"178.160.245.88")') where idcolumn= 1125;

Upvotes: 0

Views: 159

Answers (2)

Ronen Ariely
Ronen Ariely

Reputation: 2434

Check if this fit your needs

DECLARE @myDoc XML
SET @myDoc = '<GodBrandConfig>
  <AppSecret>hello</AppSecret>
  <WebClientUrl>url</WebClientUrl>
  <AllowableIpAddresses>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>.*</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>178.160.245.88</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>178.160.245.88</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
  </AllowableIpAddresses>
  <GameplaySummaryUrl>about:blank</GameplaySummaryUrl>
</GodBrandConfig>'  

-- Create clean node without duplication using "distinct-values"
DECLARE @AllowableIpAddress XML
SELECT @AllowableIpAddress = (
    SELECT @myDoc.query('
    <AllowableIpAddresses>
           {
                  for $x in distinct-values(//AllowableIpAddress/text())
                  return <AllowableIpAddress>{$x}</AllowableIpAddress>
           }
    </AllowableIpAddresses>
    ')
)

-- "replace node" is not supported in T-SQL (it is supported in oracle)
-- Therefore I will use modify with the actions "delete" and "insert"  
SET @myDoc.modify('delete (/GodBrandConfig/AllowableIpAddresses)')
SET @myDoc.modify('insert sql:variable("@AllowableIpAddress") after (/GodBrandConfig/WebClientUrl)[1]')
SELECT @myDoc ;

Upvotes: 0

AlwaysLearning
AlwaysLearning

Reputation: 8819

You need to select an AllowableIpAddress node in your XPath, and since you're wanting to remove a duplicate you can delete the second occurrence that matches the specified text using something like:

update #DemoTable
set [column].modify('delete /GodBrandConfig/AllowableIpAddresses/AllowableIpAddress[text()="178.160.245.88"][2]')
where idcolumn = 1125;

Which yields the updated XML:

<GodBrandConfig>
    <AppSecret>hello</AppSecret>
    <WebClientUrl>url</WebClientUrl>
    <AllowableIpAddresses>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>.*</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>178.160.245.88</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    </AllowableIpAddresses>
    <GameplaySummaryUrl>about:blank</GameplaySummaryUrl>
</GodBrandConfig>

Upvotes: 1

Related Questions