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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:10:58+00:00 2026-06-06T05:10:58+00:00

I have following XML, and i wish to save its data in my SQL

  • 0

I have following XML, and i wish to save its data in my SQL table. I have a table named as tblDummy it has three columns “JobID” “ItemID” “SubitemID”. there can be multiple subitemsid for particular combination of Jobid and Itemid. How can i do this?

<jobs>
   <job>
     <jobid>4711</jobid>
     <items>
     <itemid>1</itemid>
       <subitems>
        <subitemid>1</subitemid>
        <subitemid>2</subitemid>
       </subitems>
    <itemid>2</itemid>
       <subitems>
        <subitemid>7</subitemid>
        <subitemid>10</subitemid>
       </subitems>
    <itemid>9</itemid>
       <subitems>
        <subitemid>12</subitemid>
        <subitemid>16</subitemid>
       </subitems>
    </items>
   </job>  
 </jobs>
  • 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-06T05:10:59+00:00Added an answer on June 6, 2026 at 5:10 am

    As this XML is, you cannot properly parse it. You would need to change it – you should put each item with its itemid and subitems into a separate <item> node – otherwise you just have a long list of <itemid> and <subitems> nodes under your <items> main node, but you have no means of telling which <itemid> and <subitems> nodes belong together ….

    You need to change your XML to be something like this:

    <job>
       <jobid>4711</jobid>
       <items>
          <item>
             <itemid>1</itemid>
             <subitems>
                 <subitemid>1</subitemid>
                 <subitemid>2</subitemid>
             </subitems>
          </item>   
          <item>
             <itemid>2</itemid>
              <subitems>
                ......
              </subitems>
          </item>   
          ... (possibly more <item> nodes) ....
       </items>
    </job>
    

    THEN you could use basically the same code I had for your previous question – extended to cover three levels:

    CREATE PROCEDURE dbo.SaveJobs (@input XML)
    AS BEGIN
    
    ;WITH JobsData AS
    (    
        SELECT
            JobID = JobNode.value('(jobid)[1]', 'int'),
            ItemID = ItemNode.value('(itemid)[1]', 'int'),
            SubItemID = SubItemNode.value('.', 'int')
        FROM 
            @input.nodes('/jobs/job') AS TblJobs(JobNode)
        CROSS APPLY
            JobNode.nodes('items/item') AS TblItems(ItemNode)
        CROSS APPLY
            ItemNode.nodes('subitems/subitem') AS TblSubItems(SubItemNode)
    )
    INSERT INTO dbo.tblDummy(JobID, ItemID, SubItemID)
       SELECT JobID, ItemID, SubItemID
       FROM JobsData
    END
    

    Basically, you need three “lists” of XML nodes:

    • first you need the list of all <jobs>/<job> nodes to get the jobid values
    • for each of those job nodes, you will also need to get their list of nested <items>/<item> to get the itemid value
    • from each node, you then also get the list of <subitems>/<subitem>

    This will most likely work – but most likely, it will be rather slow (three nested calls to the .nodes() function!).

    Update:

    OK, so the first call @input.nodes('/jobs/job') AS TblJobs(JobNode) basically creates a “pseudo” table TblJobs with a single column JobNode and each <job> element in your XML is being stored into a row in that pseudo table – so the first row will contain this XML in it’s JobNode column:

    <job>
       <jobid>4711</jobid>
       <items>
          <item>
             <itemid>1</itemid>
             <subitems>
                 <subitemid>1</subitemid>
                 <subitemid>2</subitemid>
             </subitems>
          </item>   
          <item>
             <itemid>2</itemid>
              <subitems>
                ......
              </subitems>
          </item>   
          ... (possibly more <item> nodes) ....
       </items>
    </job>
    

    and each further row will contain the additional XML fragments for each subsequent <job> element inside <jobs>

    From each of those XML fragments, the second call

    CROSS APPLY JobNode.nodes('items/item') AS TblItems(ItemNode)
    

    again selects a list of XML fragments into a pseudo table (TblItems) with a single column ItemNode that contains the XML fragment for each <item> node inside that <job> node we’re dealing with currently.

    So the first row in this pseudo-table contains:

         <item>
             <itemid>1</itemid>
             <subitems>
                 <subitemid>1</subitemid>
                 <subitemid>2</subitemid>
             </subitems>
          </item>   
    

    and the second row will contain

          <item>
             <itemid>2</itemid>
              <subitems>
                ......
              </subitems>
          </item>   
    

    and so on.

    And then the third call – you’ve guessed it – again extracts a list of XML elements as rows into a pseudo-table – one entry for each <subitem> node in your XML fragment.

    Update #2:

    I’m new to “JobID = JobNode.value(‘(jobid)[1]’, ‘int’)” line of code

    OK – given the <Job> XML fragment that you have:

    <job>
       <jobid>4711</jobid>
       <items>
         ......
       </items>
    </job>
    

    the .value() call just executes this XPath expression (jobid) on that XML and basically gets back the <jobid>4711</jobid> snippet. It then extracts the value of that node (the inner text), and the second parameter of the .value() call defines what SQL data type to interpret this as – so it basically grabs the 4711 from the <jobid> node and interprets it as an int

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

Sidebar

Related Questions

I have an XML structure like the following: <tables> <table name=tableName1> <row ID=34 col1=data
I have following xml file: <ab> <![CDATA[ <table> <tbody> <tr> <th>abcdef</th> </tr> <tr> <p>
I have a node in my XML file containing the following: <Apple>2011-12-01T16:33:33Z</Apple> I wish
I have following xml as a response to a service not i want to
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I have following source xml <forms> <x> <y> <x-component select=foobar /> </y> </x> <component
I have the following XML document : <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE inventory SYSTEM books.dtd>
I have the following XML message: DECLARE @XML AS XML SET @XML = '<Message>
I have the following XML: http://pastebin.com/QiRK72BK which is generated in response to a REST
I have the following XML layout for an item that I put in my

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.