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

  • SEARCH
  • Home
  • 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 8715027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:44:52+00:00 2026-06-13T05:44:52+00:00

I have the following: declare @xml XML SET @xml = ‘<record priref=2 creation=2009-12-14T10:39:04> <field

  • 0

I have the following:

declare @xml XML

SET @xml = '<record priref="2" creation="2009-12-14T10:39:04">
      <field tag="aa" occ="1" lang="nl-NL">somedata</field>
      <field tag="bb" occ="1" lang="en-US">somedata</field>
      <field tag="bb" occ="2" lang="en-US">somedata</field>
      <field tag="cc" occ="1">testdata</field>
      <field tag="dd" occ="1">testdata</field>
      <field tag="ee" occ="1" lang="nl-NL">somedata</field>
      <field tag="ee" occ="1" lang="en-US">somedata</field>
    </record>' 

DECLARE @nodeCount int
DECLARE @i int

SET @i = 1

SELECT @nodeCount = @xml.value('count(/record/field/@lang)','varchar(5)') 

WHILE (@i <= @nodeCount)
BEGIN
Set @xml.modify('replace value of (/record/field/@lang)[.="en-US"][1] with "nl-NL"')

SET @i = @i + 1
END

SELECT @xml

and I would like to update the attribute @lang conditionally.

If there is a node with the same attribute value combination for ‘tag’ and ‘occ’ (like tag=ee) the attribute value for @lang has to stay unchanged.

In other cases it has to change like the above query is already doing: change ‘en-US’ into ‘nl-NL’.

Anyone has an idea how to do this? 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-06-13T05:44:53+00:00Added an answer on June 13, 2026 at 5:44 am

    This should do it

    DECLARE @xml XML;
    
    SET @xml = '<record priref="2" creation="2009-12-14T10:39:04">
          <field tag="aa" occ="1" lang="nl-NL">somedata</field>
          <field tag="bb" occ="1" lang="en-US">somedata</field>
          <field tag="dd" occ="1">testdata</field>
          <field tag="bb" occ="2" lang="en-US">somedata</field>
          <field tag="cc" occ="1">testdata</field>
          <field tag="ee" occ="1" lang="nl-NL">somedata</field>
          <field tag="ee" occ="1" lang="en-US">somedata</field>
        </record>' 
    
    DECLARE @no INT;
    DECLARE @updneeded INT;
    DECLARE nodecur CURSOR LOCAL FAST_FORWARD
    FOR
      SELECT  rn,
              CASE WHEN COUNT(1) OVER ( PARTITION BY tag, occ ) > 1 THEN 0
                   ELSE 1
              END updneeded
      FROM    ( SELECT  ROW_NUMBER() OVER ( ORDER BY node ) AS rn,
                        node.value('@tag', 'NVARCHAR(MAX)') AS tag,
                        node.value('@occ', 'NVARCHAR(MAX)') AS occ
                FROM    @xml.nodes('/record/field') x ( node )
              ) X;
    OPEN nodecur;
    WHILE ( 1 = 1 ) 
      BEGIN
        FETCH NEXT FROM nodecur INTO @no, @updneeded;
        IF ( @@FETCH_STATUS <> 0 ) BREAK;
    
        IF ( @updneeded = 1 ) 
          SET @xml.modify('replace value of (/record/field[sql:variable("@no")]/@lang)[.="en-US"][1] with "nl-NL"');
      END
    CLOSE nodecur;
    DEALLOCATE nodecur;
    
    SELECT  @xml;
    

    However, SQL is not very fast with these types of manipulations. Might be more effective to do this in the app code or put some CLR function together.

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

Sidebar

Related Questions

I have the following XML message: DECLARE @XML AS XML SET @XML = '<Message>
I have the following XML data in SQLServer 2008 R2 DECLARE @data XML SET
I have the following code: DECLARE @testDate date --SET @testDate=dateadd(wk, 5830, 0) SET @testDate='2011-09-26
I have the following query: DECLARE @Region VARCHAR(MAX) SET @Region = '' SELECT @Region
I have following tsql code in sql server 2008: declare @ID INT SET @ID
Hi i have an xml string DECLARE @XmlQue NTEXT SET @XmlQue='<?xml version=1.0 encoding=UTF-16?><Template TId=1>
I have an XML column that looks something like the following; DECLARE @str AS
I have following stored procedure: CREATE PROCEDURE testProc(IN p_idProject INTEGER) BEGIN DECLARE nowTime DATETIME;
I have the following as two ways to declare a two dimensional String array.
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id

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.