Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7083801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:13:23+00:00 2026-05-28T07:13:23+00:00

I have the following problem and hope someone could help. I have a SQL

  • 0

I have the following problem and hope someone could help.

I have a SQL Server database with a couple thousand rows. Every row consist of a column with an ID and a column with XML data.

This XML data looks something like:

<record id="1">
 <field tag="aa" occ="1" lang="nl-NL" invariant="false">Jan</field>
 <field tag="aa" occ="1" lang="en-US" invariant="false">John</field>
 <field tag="aa" occ="1" lang="de-DE" invariant="false">der Jan</field>
 <field tag="aa" occ="2" lang="nl-NL" invariant="false">Jan2</field>
 <field tag="aa" occ="2" lang="en-US" invariant="false">John2</field>
 <field tag="ab" occ="1">Something</field>
 <field tag="ac" occ="1" lang="de-DE" invariant="false">Rechnung</field>
 <field tag="ac" occ="1" lang="nl-NL" invariant="false">rekening</field>
 <field tag="ad" occ="1">Something2</field>
 <field tag="ae" occ="1" lang="nl-NL" invariant="false">stoeptegel</field>
</record>

I would like to edit this XML for every record according to the following rules:

  1. For every unique occ (occurence), tag combination only 1 @invariant attribute can be true
  2. If a has @lang=en-US attribute, then @invariant has to be ‘true’. Remaining fields with same occ, tag combination have to remain ‘false’. (like tag aa in sample code)
  3. If a has @lang=nl-NL attribute, but no @lang=en-US, then @invariant has to be ‘true’ for ‘nl-NL’. Remaining fields with same occ, tag combination have to remain ‘false’. (like tag ac in sample code)
  4. If a occ, tag combination has only 1 instance, then @invariant has to be ‘true’. So independent of @lang value. (like tag ae in sample code)

After running 1 or more SQL queries, the code should look like:

<record id="1">
 <field tag="aa" occ="1" lang="nl-NL" invariant="false">Jan</field>
 <field tag="aa" occ="1" lang="en-US" invariant="true">John</field>
 <field tag="aa" occ="1" lang="de-DE" invariant="false">der Jan</field>
 <field tag="aa" occ="2" lang="nl-NL" invariant="false">Jan2</field>
 <field tag="aa" occ="2" lang="en-US" invariant="true">John2</field>
 <field tag="ab" occ="1">Something</field>
 <field tag="ac" occ="1" lang="de-DE" invariant="false">Rechnung</field>
 <field tag="ac" occ="1" lang="nl-NL" invariant="true">rekening</field>
 <field tag="ad" occ="1">Something2</field>
 <field tag="ae" occ="1" lang="nl-NL" invariant="true">stoeptegel</field>
</record>

My problem is creating the correct SQL query, to replace all nodes for all records, according to the above rules.

So far I came up with this:

while exists 
(
select * 
from databasetable 
where xmlcolumn.exist('/record/field/@invariant[.="false"]') = 1
)

update databasetable
set xmlcolumn.modify
('replace value of (/record/field/@invariant[.="false"])[1] with "true"')

Which edits every value of @invariant into ‘true’.

Could someone help me build the correct query? Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:13:23+00:00Added an answer on May 28, 2026 at 7:13 am

    Shred your XML and use row_number() with an order by clause that orders en-US first and nl-NL second.
    Use a second row_number() to generate a unique key for each row (ID and RowNumber).
    Store the values in a table variable.
    Get the max row number and update the XML i a loop for each row number.

    declare @Tmp table
    (
      ID int, -- Primary key in databasetable
      RowNumber int,
      Tag varchar(2),
      Occ int,
      Lang varchar(5),
      Invariant bit
      primary key (ID, RowNumber)
    );
    
    with C1 as
    (
      select T.ID, -- Primary key in databasetable
             R.F.value('@tag', 'varchar(2)') as Tag,
             R.F.value('@occ', 'int') as Occ,
             R.F.value('@lang', 'varchar(5)') as Lang
      from databasetable as T
        cross apply T.xmlcolumn.nodes('/record/field') as R(F)
    ), 
    C2 as
    (
      select ID, Tag, Occ, Lang,
             row_number() over(partition by ID order by (select 0)) as RowNumber,
             row_number() over(partition by ID, Tag, Occ 
                               order by case Lang 
                                          when 'en-US' then 1
                                          when 'nl-NL' then 2
                                          else 3
                                        end) as rnInv
      from C1
    )
    insert into @Tmp (ID, RowNumber, Tag, Occ, Lang, Invariant)
    select ID, RowNumber, Tag, Occ, Lang, case rnInv when 1 then 1 else 0 end
    from C2;
    
    declare @MaxRowNum int;
    declare @I int = 1;
    
    select @MaxRowNum = max(RowNumber)
    from @Tmp;
    
    while @I <= @MaxRowNum
    begin
      update T
      set xmlcolumn.modify('replace value of (/record/field[@tag = sql:column("Tmp.Tag") and
                                                            @occ = sql:column("Tmp.Occ") and
                                                            @lang = sql:column("Tmp.Lang")]/@invariant)[1] 
                              with sql:column("Tmp.Invariant")')
      from databasetable as T
        inner join @Tmp as Tmp
          on T.ID = Tmp.ID
      where Tmp.RowNumber = @I;
    
      set @I += 1;
    end
    

    A working sample can be found here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem and I hope someone will be able to help
I have the following problem and whatever I try, nothing helps. I hope someone
Google/Bing didnt bring up any solution to my following problem, hope someone can help
I have a small problem here and hope that someone can help me out.
Ok I got a strange issue that I hope someone could help with I
I really hope someone can shed some light on this problem, I have built
I was wondering if sanyone could help me with the following: I have some
I have following problem: I have built a tabbar application with 4 tabs. I
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
I have the following problem: I have an HTML textbox ( <input type=text> )

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.