我正在尝试将XML文件导入SQL Server数据库。请查看代码和xml文件。提前致谢。
我试图google很多页面但是所有这些页面只涉及一个孩子(或者如何调用它)。
但在我的真实xml中,结构就像
:1x supplier --> multiple orders --> multiple customers --> multiple products.
我们需要创建一个存储过程,当我们获取XML文件时将调用该存储过程。
再次感谢Petr
SQL代码:
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文件:
<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>
答案 0 :(得分:1)
这个怎么样:
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)
这会“粉碎”XML并使用嵌套的CROSS APPLY .nodes()
方法从订单节点中提取任意数量的客户,以及任意数量的产品。