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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:39:10+00:00 2026-06-12T17:39:10+00:00

I’m trying to import an XML file into a SQL Server database. Please have

  • 0

I’m trying to import an XML file into a SQL Server database. Please have a look on the code and on the xml file. Thanks in advance.

I tried to google lot of pages but all of them deal with only one child (or how to call it).

But In my real xml the structure is like

:1x supplier --> multiple orders --> multiple customers --> multiple products. 

We need to create a stored procedure which will be called when we get the XML file.

Thanks again Petr

SQL code:

DROP TABLE #TEST

CREATE TABLE #TEST
(CUSTID VARCHAR(10),CUSTNAME VARCHAR(50),PRODUCT VARCHAR(500))

INSERT INTO #TEST(CUSTID,CUSTNAME,PRODUCT)
SELECT
    x.orders.query('order/customer/cust_id').value('.', 'VARCHAR(10)'),
    x.orders.query('order/customer/name').value('.', 'VARCHAR(50)'),
    x.orders.query('order/product_lines/product/supplier_prod_num').
   value('.','VARCHAR(500)') 
FROM
   (SELECT CAST(x AS XML)
    FROM OPENROWSET(
          BULK '\\test\Test.xml',
          SINGLE_BLOB) AS T(x)) AS T(x) 
    CROSS APPLY
        x.nodes('order_batch/orders/order/product_lines/product') AS x(orders)

select * from #test

XML file to test:

<order_batch>
  <order_header>
    <doc_format_ver>1.0</doc_format_ver>
    <originating_software>
      <software_name>test</software_name>
      <software_ver>2.0</software_ver>
    </originating_software>
  </order_header>
  <supplier>
    <cp_supplier_id>93</cp_supplier_id>
    <name>supname</name>
    <address_line_1>address_line_1</address_line_1>
    <address_line_2>address_line_1</address_line_2>
  </supplier>
  <orders>
    <order o_count="1">
      <customer>
        <cust_id>7240</cust_id>
        <name>CustNamek</name>
        <address_line_1>address_line_1</address_line_1>
        <address_line_2>address_line_1</address_line_2>
      </customer>
      <order_details>
        <date_created>2012-10-08</date_created>
        <time_created>05:37:11</time_created>
      </order_details>
      <product_lines>
        <product p_count="1">
          <supplier_prod_num>15115</supplier_prod_num>
          <qty_type>E</qty_type>
        </product>
        <product p_count="2">
          <supplier_prod_num>010211</supplier_prod_num>
          <qty_type>E</qty_type>
        </product>
      </product_lines>
    </order>
    <order o_count="2">
      <customer>
        <cust_id>7238</cust_id>
        <name>custname</name>
        <address_line_1>address_line_1</address_line_1>
        <address_line_2>address_line_1</address_line_2>
      </customer>
      <order_details>
        <supplier_ref>0093513982</supplier_ref>
        <delivery_date>2012-10-08</delivery_date>
        <special_instructions />
      </order_details>
      <product_lines>
        <product p_count="1">
          <supplier_prod_num>6748</supplier_prod_num>
          <qty_type>E</qty_type>
        </product>
        <product p_count="2">
          <supplier_prod_num>6744</supplier_prod_num>
          <qty_type>E</qty_type>
        </product>
      </product_lines>
    </order>
  </orders>
</order_batch>
  • 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-12T17:39:12+00:00Added an answer on June 12, 2026 at 5:39 pm

    How about this:

    CREATE TABLE #TEST (CUSTID VARCHAR(50), CUSTNAME VARCHAR(50), PRODUCT VARCHAR(500))
    
    DECLARE @input XML 
    
    SELECT @input = CAST(x AS XML)
    FROM OPENROWSET
         (BULK '\\test\Test.xml',
          SINGLE_BLOB) AS T(X)
    
    INSERT INTO #TEST(CUSTID, CUSTNAME, PRODUCT)
        SELECT
            Customers.value('(cust_id)[1]', 'VARCHAR(50)'),
            Customers.value('(name)[1]', 'VARCHAR(50)'),
            Products.value('(supplier_prod_num)[1]', 'VARCHAR(500)')
        FROM
            @input.nodes('/order_batch/orders/order') AS Tbl(Orders)
        CROSS APPLY
            Orders.nodes('customer') AS TblC(Customers)
        CROSS APPLY
            Orders.nodes('product_lines/product') AS TblP(Products)
    

    This “shreds” the XML and uses nested CROSS APPLY .nodes() methods to extract any number of customers from the order node, and any number of products, too.

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

Sidebar

Related Questions

I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

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.