I am testing a script I have just written to import some sample xml data into a SQL table.
The import is done in two stages: the first stage imports the xml file into a sql table and the second, parses out the file into a relational format and then imports it into a second table.
The imported data should look as follows:
orderID CustID OrderDate ProdID Qty
10248 VINET Jul 4 1996 12:00AM 11 12
10248 VINET Jul 4 1996 12:00AM 42 10
10283 LILAS Aug 16 1996 12:00AM 72 3
The script works but fails to import the data in the second row. In other words, the customer with orderID number 10248 ordered two products but the script is not importing the product with ProdID number 42.
How do I modify the script to import this record?
Here is the XML data (E:\Extracts\XMLData\customers.xml):
<ROOT>
<Customer CustomerID="VINET" ContactName="Paul Henriot">
<Order OrderID="10248" CustomerID="VINET" EmployeeID="5"
OrderDate="1996-07-04T00:00:00">
<OrderDetail ProductID="11" Quantity="12"/>
<OrderDetail ProductID="42" Quantity="10"/>
</Order>
</Customer>
<Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
<Order OrderID="10283" CustomerID="LILAS" EmployeeID="3"
OrderDate="1996-08-16T00:00:00">
<OrderDetail ProductID="72" Quantity="3"/>
</Order>
</Customer>
</ROOT>
Here is the script:
-- Stage 1. Import XML file into a SQL table
CREATE TABLE XmlImportTest
(
xmlFileName VARCHAR(300), -- To store xml file name
xml_data XML -- To store actual xml data
)
DECLARE @xmlFileName VARCHAR(300)
SELECT @xmlFileName = 'E:\Extracts\XMLData\customers.xml'
EXEC('
INSERT INTO XmlImportTest(xmlFileName, xml_data)
SELECT ''' + @xmlFileName + ''', xmlData
FROM
(
SELECT *
FROM OPENROWSET (BULK ''' + @xmlFileName + ''' , SINGLE_BLOB) AS XMLDATA
) AS FileImport (XMLDATA)
')
SELECT * FROM XmlImportTest
-- Stage 2. Parse out XML file into a relational format and then import it into another table
CREATE TABLE [dbo].[tbl_XML_Customer](
[OrderID] [int] NULL,
[CustomerID] [varchar](10) NULL,
[OrderDate] [datetime] NULL,
[ProdID] [int] NULL,
[Qty] [int] NULL
)
INSERT INTO [tbl_XML_Customer](
[OrderID],
[CustomerID],
[OrderDate],
[ProdID],
[Qty]
)
SELECT tab.col.value('./Order[1]/@OrderID','int') AS 'OrderID',
tab.col.value('./@CustomerID[1]','varchar(10)') AS 'CustomerID',
tab.col.value('./Order[1]/@OrderDate','datetime') AS 'OrderDate',
tab.col.value('./Order[1]/OrderDetail[1]/@ProductID','int') AS 'ProductID',
tab.col.value('./Order[1]/OrderDetail[1]/@Quantity','int') AS 'Quantity'
FROM [XmlImportTest]
CROSS APPLY
XML_Data.nodes('//Customer') AS tab(col)
SELECT *
FROM [tbl_XML_Customer]
Thank you so much in advance for your help.
jcad1
The import is done in two stages: the first stage imports the xml file into a sql table and the second, parses out the file into a relational format and then imports it into a second table.
The imported data should look as follows:
orderID CustID OrderDate ProdID Qty
10248 VINET Jul 4 1996 12:00AM 11 12
10248 VINET Jul 4 1996 12:00AM 42 10
10283 LILAS Aug 16 1996 12:00AM 72 3
The script works but fails to import the data in the second row. In other words, the customer with orderID number 10248 ordered two products but the script is not importing the product with ProdID number 42.
How do I modify the script to import this record?
Here is the XML data (E:\Extracts\XMLData\customers.xml):
<ROOT>
<Customer CustomerID="VINET" ContactName="Paul Henriot">
<Order OrderID="10248" CustomerID="VINET" EmployeeID="5"
OrderDate="1996-07-04T00:00:00">
<OrderDetail ProductID="11" Quantity="12"/>
<OrderDetail ProductID="42" Quantity="10"/>
</Order>
</Customer>
<Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
<Order OrderID="10283" CustomerID="LILAS" EmployeeID="3"
OrderDate="1996-08-16T00:00:00">
<OrderDetail ProductID="72" Quantity="3"/>
</Order>
</Customer>
</ROOT>
Here is the script:
-- Stage 1. Import XML file into a SQL table
CREATE TABLE XmlImportTest
(
xmlFileName VARCHAR(300), -- To store xml file name
xml_data XML -- To store actual xml data
)
DECLARE @xmlFileName VARCHAR(300)
SELECT @xmlFileName = 'E:\Extracts\XMLData\customers.xml'
EXEC('
INSERT INTO XmlImportTest(xmlFileName, xml_data)
SELECT ''' + @xmlFileName + ''', xmlData
FROM
(
SELECT *
FROM OPENROWSET (BULK ''' + @xmlFileName + ''' , SINGLE_BLOB) AS XMLDATA
) AS FileImport (XMLDATA)
')
SELECT * FROM XmlImportTest
-- Stage 2. Parse out XML file into a relational format and then import it into another table
CREATE TABLE [dbo].[tbl_XML_Customer](
[OrderID] [int] NULL,
[CustomerID] [varchar](10) NULL,
[OrderDate] [datetime] NULL,
[ProdID] [int] NULL,
[Qty] [int] NULL
)
INSERT INTO [tbl_XML_Customer](
[OrderID],
[CustomerID],
[OrderDate],
[ProdID],
[Qty]
)
SELECT tab.col.value('./Order[1]/@OrderID','int') AS 'OrderID',
tab.col.value('./@CustomerID[1]','varchar(10)') AS 'CustomerID',
tab.col.value('./Order[1]/@OrderDate','datetime') AS 'OrderDate',
tab.col.value('./Order[1]/OrderDetail[1]/@ProductID','int') AS 'ProductID',
tab.col.value('./Order[1]/OrderDetail[1]/@Quantity','int') AS 'Quantity'
FROM [XmlImportTest]
CROSS APPLY
XML_Data.nodes('//Customer') AS tab(col)
SELECT *
FROM [tbl_XML_Customer]
Thank you so much in advance for your help.
jcad1