Solved

Import XML into SQL Server 2008

Posted on 2014-01-18
20
1,725 Views
Last Modified: 2014-02-10
Get XML FILE

Import into 2 Tables in :  SQL SERVER 2008

...............................

The First Problem : I am not good with XML files , I am good with SQL procs, views, functions, .... I don't mess with XML, but now I need to learn how

The Second Problem : I am having trouble with getting the XML data into a table using SQL Server
.
The Third Problem : I need to somehow LOOP the embedded envelope data [LineItemList] ... where there is always a [ChildCount] ... and then follows the [LineItems]

 
There is an ID number :  11213696
And there is an X-Number of Line Items

-      1. Import the Main Data into a Table :  
                o      [PurchReqNumb]
                o      [SolicitationNumb]
                o      [ReturnByDate]

-      2. Import the Line Items into a Second Table with a referencing data point
                o      Requires adding the [GovtRFQ  ID]  to the second table for a reference
                o      Add the Line Item details to fields in a second table

...Below is  pulled out of XML File ...
...I have attached the XML file in case you need to see it that way ...


<GovtRFQ ID="11213696">
       <Received>20131011 11:27</Received>
       <PurchReqNumb>00506802-AA</PurchReqNumb>
       <SolicitationNumb>SPM2DP-AA-Q-0005</SolicitationNumb>
       <ReturnByDate>10/15/2013</ReturnByDate>
       <LineItemList ChildCount="3">
            <LineItem CLIN="0001">
                  <NSN>6505002998015</NSN>
                  <Quantity>2</Quantity>
           </LineItem>
           <LineItem CLIN="0002">
                 <NSN>6505002998025</NSN>
                 <Quantity>1</Quantity>
           </LineItem>
           <LineItem CLIN="0003">
                 <NSN>6505002998035</NSN>
                 <Quantity>9</Quantity>
          </LineItem>
     </LineItemList>
</GovtRFQ>
ReqFileXML.xml
0
Comment
Question by:PAGANED
  • 10
  • 5
  • 5
20 Comments
 
LVL 18

Expert Comment

by:zc2
Comment Utility
Do you have the XSD Schema file to your XML ? if you do, you can use the XMLSQLBulkLoad component to import the XML to the SQL.
See examples here:
http://technet.microsoft.com/en-us/library/ms171806.aspx
0
 
LVL 1

Author Comment

by:PAGANED
Comment Utility
I do not have an XSD file
.
Rather than use a VBScript , isnt' there some way to do this via a Procedure ..?
.
I was thinking of something like a SQL Procedure:
.
.
Create a Temp Table with an XML field
Import XML file text into that #TempTable.Field
Select that #Temp.Table.Field
Process by using INSERT INTO for SQL.Table1
Loop the Embedded groups using INSERT INTO for SQL.Table2
..
Process all GovtRFQs
.
.
0
 
LVL 18

Expert Comment

by:zc2
Comment Utility
I'm sorry, no knowledge about SQL abilities to load XML. But I guess if there were any ones Microsoft would not bothered to create the additional component to do the job.

XSD schema could be crafted out of an existed XML example. It's not an easy task for a somebody who never deal with XML/XSD, but that's possible.
0
 
LVL 1

Author Comment

by:PAGANED
Comment Utility
This looks interesting :  If I was able to put everything into a single table then I could use different routines to get the data where I wanted
.
.

CREATE Temp TABLE #XMLtable
(
Id INT IDENTITY PRIMARY KEY,
XMLData XML
)


INSERT INTO #XMLtable(XMLData)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn
FROM OPENROWSET(BULK 'C:\ReqFileXML.xml', SINGLE_BLOB) AS x;



DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)

SELECT @XML = XMLData FROM #XMLtable

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

SELECT PurchReqNumb, SolicitationNumb, ReturnByDate, ChildCount, CLIN, NSN, Quantity
FROM OPENXML(@hDoc, 'GovtRFQ/LineItemList')
WITH
(
PurchReqNumb [varchar](50) '../@PurchReqNumb',
SolicitationNumb [varchar](50) '../@SolicitationNumb',
ReturnByDate datetime '../@ReturnByDate',
ChildCount int '@ChildCount',
CLIN [varchar](10) '@CLIN',
NSN [varchar](50) '@NSN',
Quantity int '@Quantity'
)

EXEC sp_xml_removedocument @hDoc
GO
.
.
0
 
LVL 18

Expert Comment

by:zc2
Comment Utility
This is great.
0
 
LVL 1

Author Comment

by:PAGANED
Comment Utility
I am close but I can't get the data
I am doing something wrong
.
.

SELECT ID, PurchReqNumb, SolicitationNumb, ReturnByDate
FROM OPENXML(@hDoc, 'GovtRFQDocs/GovtRFQ')
WITH
(
ID int '@ID',
PurchReqNumb [varchar](50) '@PurchReqNumb',
SolicitationNumb [varchar](50) '@SolicitationNumb',
ReturnByDate datetime '@ReturnByDate'
)

11213696 : ID
NULL :   PurchReqNumb
NULL :   SolicitationNumb
NULL :   ReturnByDate
0
 
LVL 18

Assisted Solution

by:zc2
zc2 earned 300 total points
Comment Utility
Just a guess.
@PurchReqNumb means an attribute, but the PurchReqNumb is an element in the input XML. So, try to remove the '@' character.
0
 
LVL 1

Author Comment

by:PAGANED
Comment Utility
It seems that I can NOT grab some of the data fields ..
..
frustrating
..
.

11213696  : ID
NULL :   PurchReqNumb
NULL :   SolicitationNumb
NULL :   ReturnByDate
  3      :  ChildCount
0001  :  CLIN
NULL  : NSN
NULL  :  Quantity

.



SELECT ID, PurchReqNumb, SolicitationNumb, ReturnByDate, ChildCount, CLIN, NSN, Quantity
FROM OPENXML(@hDoc, 'GovtRFQDocs/GovtRFQ/LineItemList/LineItem')
WITH
(
ID int '../../@ID',
PurchReqNumb [varchar](50) '../../@PurchReqNumb',
SolicitationNumb [varchar](50) '../../@SolicitationNumb',
ReturnByDate datetime '../../@ReturnByDate',
ChildCount int '../@ChildCount',
CLIN [varchar](10) '@CLIN',
NSN [varchar](50) '@NSN',
Quantity int '@Quantity'
)
0
 
LVL 18

Expert Comment

by:zc2
Comment Utility
Why you did not try my suggestion?
0
 
LVL 1

Author Comment

by:PAGANED
Comment Utility
YES ...  ZC2
.
.
Just a guess.
@PurchReqNumb means an attribute, but the PurchReqNumb is an element in the input XML. So, try to remove the '@' character.
.
.

ID      PurchReqNumb      SolicitationNumb      ReturnByDate      ChildCount      CLIN      NSN      Quantity
11213696      00506802-01      SPM2DP-14-Q-0003      10/15/2013      3      0001      6505002998095      2
11213696      00506802-01      SPM2DP-14-Q-0003      10/15/2013      3      0002      6505002998095      1
11213696      00506802-01      SPM2DP-14-Q-0003      10/15/2013      3      0003      6505002998095      9
.
.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:PAGANED
Comment Utility
I think this does it for this question  ...  ZC2
.
I will wait to close the question until I get a full XML file and see if I run into any other problems
.
.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Just a word of caution, OPENXML() is the old way to retrieve information from an Xml document.  With SQL Server 2005 and above you may find the Xml Data Type Methods a better option.
0
 
LVL 1

Accepted Solution

by:
PAGANED earned 0 total points
Comment Utility
I am using Windows Server 2008
.
.
I would love to try another method of extracting the XML data
Could you recreate the following code using XQuery or some other SQL way ?

.
.
I am listing my currently working Code with OpenXML
.
.
       CREATE  TABLE #XMLtable
	(Id INT IDENTITY PRIMARY KEY,
	XMLData XML)

	INSERT INTO #XMLtable(XMLData)
	SELECT CONVERT(XML, BulkColumn) AS BulkColumn
	FROM OPENROWSET(BULK 'C:\ReqFileXML.xml', SINGLE_BLOB) AS x;

	DECLARE @XML AS XML, @hDoc AS INT
	SELECT @XML = XMLData FROM #XMLtable
	DROP Table #XMLtable

	EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

	SELECT ID, PurchReqNumb, SolicitationNumb, ReturnByDate, ChildCount, CLIN, NSN, Quantity ,CAGECode
	FROM OPENXML(@hDoc, 'GovtRFQDocs/GovtRFQ/LineItemList/LineItem')
	WITH 
	(
	ID int '../../@ID',
	PurchReqNumb [nvarchar](50) '../../PurchReqNumb',
	SolicitationNumb [nvarchar](50) '../../SolicitationNumb',
	ReturnByDate [nvarchar](50) '../../ReturnByDate',
	ChildCount int '../@ChildCount',
	CLIN [varchar](10) '@CLIN',
	NSN [varchar](50) 'NSN',
	Quantity [varchar](50) 'Quantity',
	CAGECode [nvarchar](10) '../../AppdPNList/AppdPNItem/CAGECode'
	)

	EXEC sp_xml_removedocument @hDoc

Open in new window

0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 200 total points
Comment Utility
You could replace the sp_xml_preparedocument , OPENXML, sp_xml_removedocument  portion with this code:

SELECT	T.c.value('../../@ID[1]', 'integer') ID,
	T.c.value('../../PurchReqNumb[1]', 'nvarchar(50)') PurchReqNumb,
	T.c.value ('../../SolicitationNumb[1]', 'nvarchar(50)') SolicitationNumb ,
	T.c.value('../../ReturnByDate[1]', 'nvarchar(50)') ReturnByDate,
	T.c.value('../@ChildCount', 'integer'),
	T.c.value('@CLIN', 'varchar(10)') CLIN,
	T.c.value('NSN[1]', 'varchar(50)') NSN,
	T.c.value('Quantity[1]', 'varchar(50)') Quantity,
	T.c.value ('../../AppdPNList[1]/AppdPNItem[1]/CAGECode[1]', 'nvarchar(10)') CAGECode
FROM @Xml.nodes('GovtRFQDocs/GovtRFQ/LineItemList/LineItem') T(c)

Open in new window

0
 
LVL 1

Author Comment

by:PAGANED
Comment Utility
Very Cool... thank you
.
Over the same XML data of 565 rows :

- OPENRECORDSET takes 1 Second

- XQuery NODES takes 10 seconds
.
That's a huge hit on performance based on such a small recordset
Do I need to activate something for better performance ?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Do you mean OPENXML takes 1 second compared to Xml Data Type Methods taking 10 seconds?
0
 
LVL 1

Author Comment

by:PAGANED
Comment Utility
The OPENXML code takes 1 second to run 565 rows
the NODES code takes 10 seconds to run 565 rows
.
I expected no difference in execution time, but there it was
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Just wanted to clarify as you had stated "OPENRECORDSET takes 1 Second".

If your Xml is that small, there is not much traffic on this server and speed is important to you I would stick with OPENXML.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
using XQuery, which SHOULD BE newer and cooler, but all it is is newer since it's so slow.
I am afraid you are desperately missing the point.  Here is what I stated:
If your Xml is that small, there is not much traffic on this server and speed is important to you I would stick with OPENXML.
There is a reason OPENXML() in some circumstances is faster and there is also a reason why XQuery was introduced to replace it.
0
 
LVL 1

Author Closing Comment

by:PAGANED
Comment Utility
I almost had it, and ZC2's comment about differences between Elements and Attributes while utilizing the @ character finally got good data returns as they should be
.
Anthony Perkins is using XQuery, which SHOULD BE newer and cooler, but all it is is newer since it's so slow. Anyway, it's another way to do the same thing I'm trying to do so thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now