将数据从XML文件导入和处理到SQL Server表中
使用OPENROWSET从XML文件导入XML数据
我有一个从FTP位置下载到本地文件夹的XML文件,该XML文件中的数据如下所示。
现在,为了将数据从XML文件导入到SQL Server中的表中,我正在使用OPENROWSET函数,如下所示。
在下面的脚本中,我首先创建一个带有一列数据类型为XML的表,然后使用OPENROWSET函数通过指定XML文件的文件位置和名称从文件中读取XML数据,如下所示:
CREATE DATABASE OPENXMLTesting GO USE OPENXMLTesting GO CREATE TABLE XMLwithOpenXML ( Id INT IDENTITY PRIMARY KEY, XMLData XML, LoadedDateTime DATETIME ) INSERT INTO XMLwithOpenXML(XMLData, LoadedDateTime) SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() FROM OPENROWSET(BULK 'D:\OpenXMLTesting.xml', SINGLE_BLOB) AS x; SELECT * FROM XMLwithOpenXML
查询导入了XML数据的表时,它看起来像这样。XMLData列是XML数据类型,它将输出超链接,如下所示:
单击上图中的超链接,将在SSMS中打开另一个选项卡,其中显示XML数据,如下所示。
<ROOT> <Customers> <Customer CustomerName="Arshad Ali" CustomerID="C001"> <Orders> <Order OrderDate="2012-07-04T00:00:00" OrderID="10248"> <OrderDetail Quantity="5" ProductID="10" /> <OrderDetail Quantity="12" ProductID="11" /> <OrderDetail Quantity="10" ProductID="42" /> </Order> </Orders> <Address> Address line 1, 2, 3</Address> </Customer> <Customer CustomerName="Paul Henriot" CustomerID="C002"> <Orders> <Order OrderDate="2011-07-04T00:00:00" OrderID="10245"> <OrderDetail Quantity="12" ProductID="11" /> <OrderDetail Quantity="10" ProductID="42" /> </Order> </Orders> <Address> Address line 5, 6, 7</Address> </Customer> <Customer CustomerName="Carlos Gonzlez" CustomerID="C003"> <Orders> <Order OrderDate="2012-08-16T00:00:00" OrderID="10283"> <OrderDetail Quantity="3" ProductID="72" /> </Order> </Orders> <Address> Address line 1, 4, 5</Address> </Customer> </Customers> </ROOT>
使用OPENXML函数处理XML数据
USE OPENXMLTesting GO DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX) SELECT @XML = XMLData FROM XMLwithOpenXML EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML SELECT CustomerID, CustomerName, Address FROM OPENXML(@hDoc, 'ROOT/Customers/Customer') WITH ( CustomerID [varchar](50) '@CustomerID', CustomerName [varchar](100) '@CustomerName', Address [varchar](100) 'Address' ) EXEC sp_xml_removedocument @hDoc GO
如果要导航回父级或祖级父级并从那里获取数据,则需要使用“ ../”读取父级数据,并使用“ ../../”读取祖级父级数据
USE OPENXMLTesting GO DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX) SELECT @XML = XMLData FROM XMLwithOpenXML EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML SELECT CustomerID, CustomerName, Address, OrderID, OrderDate FROM OPENXML(@hDoc, 'ROOT/Customers/Customer/Orders/Order') WITH ( CustomerID [varchar](50) '../../@CustomerID', CustomerName [varchar](100) '../../@CustomerName', Address [varchar](100) '../../Address', OrderID [varchar](1000) '@OrderID', OrderDate datetime '@OrderDate' ) EXEC sp_xml_removedocument @hDoc GO
USE OPENXMLTesting GO DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX) SELECT @XML = XMLData FROM XMLwithOpenXML EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML SELECT CustomerID, CustomerName, Address, OrderID, OrderDate, ProductID, Quantity FROM OPENXML(@hDoc, 'ROOT/Customers/Customer/Orders/Order/OrderDetail') WITH ( CustomerID [varchar](50) '../../../@CustomerID', CustomerName [varchar](100) '../../../@CustomerName', Address [varchar](100) '../../../Address', OrderID [varchar](1000) '../@OrderID', OrderDate datetime '../@OrderDate', ProductID [varchar](50) '@ProductID', Quantity int '@Quantity' ) EXEC sp_xml_removedocument @hDoc GO
Thanks & Best Regards!
Javi Zhu 朱佳辉
Mobile: 15900467108
Email: Javi.zhu@outlook.com