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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:39:25+00:00 2026-05-20T05:39:25+00:00

This is my xml <root> <children> <child id = 1> <description>This is child 1</description>

  • 0

This is my xml

<root>
<children>
 <child id = "1">
  <description>This is child 1</description>
 </child>
 <child id = "2">
    <description>This is child 2</description>
 </child>
 <child id = "3">
    <description>This is child 3</description>
 </child>
</children>
</root>

I am trying to update a table named Child which has columns “ID” and “Description”. The table already has the ID column values in it but the description is blank. I need to update this description against the ID from the above given xml file.

I tried doing a OPENXML with flag value as 2 (element-centric) and was able to retrieve all the descriptions.
But am unaware as in how to retrieve descriptions based on the ID values, using a where clause in OPENXML.

The database I am using is SQL Server 2008.
(Also will OPENXML work for SQL Server 2005?)

This is wat I tried to do:

DECLARE @idoc int DECLARE @doc xml

select @doc= c from openrowset(bulk 'C:\Test.xml',single_blob) as temp(c)
exec sp_xml_preparedocument @idoc output, @doc 

SELECT * FROM OPENXML (@idoc, '/root/children/child', 2)
  WITH (summary  varchar(1000)) descr 
EXEC sp_xml_removedocument @idoc

Thanks in advance
Nick

p.s: The structure of the xml cannot be changed. I need to work around with this constraint.

  • 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-20T05:39:25+00:00Added an answer on May 20, 2026 at 5:39 am

    Assuming you’re on SQL Server 2005 or up and you’re XML is stored in a variable called @input, you could try this:

    declare @input XML 
    
    set @input = '<root>
    <children>
     <child id="1">
      <description>This is child 1</description>
     </child>
     <child id="2">
        <description>This is child 2</description>
     </child>
     <child id="3">
        <description>This is child 3</description>
     </child>
    </children>
    </root>'
    
    ;with GrabXML AS
    (
        select
            child.value('@id', 'int') as 'ID',
            child.value('(description)[1]', 'varchar(50)') as 'Description'
        from
            @input.nodes('/root/children/child') as n(Child)
    )
    update dbo.Child
    set description = g.Description
    from GrabXML g
    where dbo.Child.ID = g.ID
    

    The GrabXML common table expression (CTE) parses and flattens the XML into rows and columns, and the following UPDATE statement uses those rows/columns to update the existing table.

    Update: there are two possible problem points if your strings are longer than you expect:

    first off, you might need to increase the length on the conversion from XML:

    select
       child.value('@id', 'int') as 'ID',
       child.value('(description)[1]', 'varchar(999)') as 'Description'
    

    and secondly, you need to check what length your column in the table dbo.Child is and truncate the length of the string extracted from the XML to not exceed the length of the database column:

    update dbo.Child
    set description = SUBSTRING(g.Description, 1, 575)
    

    Here, you need to extract at most as many characters from the g.Description string as your database column dbo.Child.description allows.

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

Sidebar

Related Questions

Say I have this given XML file: <root> <node>x</node> <node>y</node> <node>a</node> </root> And I
Greetings! I have some XML like this: <Root> <AlphaSection> . . . </AlphaSection> <BetaSection>
Greetings! I have some XML like this: <Root> <MainSection> <SomeNode>Some Node Value</SomeNode> <SomeOtherNode>Some Other
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>
If I had some XML such as this loaded into an XDocument object: <Root>
I am parsing some XML something like this: <root> <some_gunk/> <dupe_node> ... stuff I
I have a piece of XML that is structured similar to this: <root> <score
I've got some XML, for example purposes it looks like this: <root> <field1>test</field1> <f2>t2</f2>
Greetings! If I have XML such as this: <Root> <AlphaSection> . . . </AlphaSection>
Given the following XML: <cfsavecontent variable=xml> <root> <parent> <child>I'm the first</child> <child>Second</child> <child>3rd</child> </parent>

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.