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 8265835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:56:31+00:00 2026-06-08T04:56:31+00:00

I have an XML variable that looks like this: <code> <IDs> <ID id=1>a</ID> <ID

  • 0

I have an XML variable that looks like this:

<code>
<IDs>
    <ID id="1">a</ID>
    <ID id="43">d</ID>
    <ID id="3">b</ID>

</IDs>
</code>

I want to use that in a stored procedure (SQL Server) that will update a table.

My table look like this:

ID INT,
a INT,
b INT,
c INT,
d INT

The statement should increase the letter value associated with the id.

SO it would look like this:

Table Row with ID = 1, update column "a" by increasing the current value by 1.
Table Row with ID = 43 - update column "d" by increasing current value by 1.
Finally Table row with ID= 3 - update column "b" by increasing value by 1.

This is what I have so far – (The second line is where i need the most help.):

Update MyTable
SET @letter = letterVal +1
WHERE ID IN(
SELECT x.v.value('@id','INT')
FROM @xmlIDs.nodes('/IDs/ID') x(v)
)
  • 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-08T04:56:34+00:00Added an answer on June 8, 2026 at 4:56 am

    You would have to do something along the lines of this:

    DECLARE @input XML = '<code>
    <IDs>
        <ID id="1">a</ID>
        <ID id="43">d</ID>
        <ID id="3">b</ID>
    
    </IDs>
    </code>'
    
    ;WITH ParsedXML AS
    (
    SELECT
        ID = C.value('(@id)[1]', 'int'),
        ColumnName = C.value('(.)[1]', 'varchar(10)')
    FROM @Input.nodes('/code/IDs/ID') AS T(C)
    )
    UPDATE MyTable 
    SET a = CASE WHEN p.ColumnName = 'a' THEN t.a + 1 ELSE t.a END,
        b = CASE WHEN p.ColumnName = 'b' THEN t.b + 1 ELSE t.b END,
        c = CASE WHEN p.ColumnName = 'c' THEN t.c + 1 ELSE t.c END,
        d = CASE WHEN p.ColumnName = 'd' THEN t.d + 1 ELSE t.d END
    FROM MyTable t
    INNER JOIN ParsedXml p ON t.ID = p.ID
    
    SELECT * FROM Mytable
    

    This will do it – but it’s really quite ugly. The main problem is: you cannot get the column name as a value from somewhere else, to use it in the UPDATE statement – unless you go the dynamic SQL route, which has its own set of pros and cons and can get rather messy.

    If you’re interested in dynamic SQL – The Curse and Blessings of Dynamic SQL by Erland Sommarskog is an absolute must-read – read it before you launch into using dynamic SQL!

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

Sidebar

Related Questions

I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
So I have a xml document loaded in JavaScript variable. Xml looks like this:
Let's say I have an XML file that looks like this: <a id=x> <b>
I have a xml that looks like this, <Parent> Running text with marked up
Alright I have an xml document that looks something like this: <xml> <list> <partner>
I want to write a stored procedure that queries XML files after I have
Imagine I have some XML that looks like this: DECLARE @xml XML SET @xml
I have some xml that looks like this: <xml><name>oscar</name><race>puppet</race><class>grouch</class></xml> The tags change and are
I have some XML that looks something like this: <ExtensionObject> <Value xmlns=> <Key>key01</Key> <StringValue>somewords</StringValue>
I have this code that is supposed to load the attributes of an XML

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.