XML file import to SQL database with multiple children
-
07-07-2021 - |
Question
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>
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow