Solved

Import XML file into SQL 2000

Posted on 2010-09-13
5
688 Views
Last Modified: 2012-05-10
Greetings,

I searched the tried to find an existing answer that could help but due to my limited knowledge of importing xml could not find a solution that would work.

Our engineers use solidworks which when a part is "approved" will generate an XML file with information regarding the part.

What i am looking for is either a sql procedure or code for visual studio that will take the values in the XML file and import them into a temporary table in SQL 2000.

The included XML file is an example of what is exported by Solidworks, at this point we are only concerned with the part itself and not the rest of the XML which is the BOM for the part.  Everything under <references> is the bom which we do not need at this point.

I only need the following attributes from the first part (part itself) to be added as columns in the SQL temp table.
FDESCRIPT
FSTDMEMO
FREV
Vendor
FPRODCL
FPARTNO
FGROUP
Stock Usage
Stock Description
Stock Number
FSOURCE
FMEASURE



Please let me know if i missed any important information and thank you all very much!!!



TESTASM1-X.XML
0
Comment
Question by:SynsorIT
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33668624
You would use something like this:
DECLARE @Xml varchar(8000)

SET @Xml = '
<xml>
	<transactions>
		<transaction date="1284414935" type="wf_export_document_attributes" vaultname="Test">
			<document aliasset="ERP" id="TESTASM1" idattribute="Part Number" idcfgname="Default" pdmweid="9894">
				<configuration name="Default" quantity="1">
					<attribute name="FDESCRIPT" value="XXXASM1" /> 
					<attribute name="FSTDMEMO" value="" /> 
					<attribute name="FREV" value="X" /> 
					<attribute name="Vendor" value="" /> 
					<attribute name="FPRODCL" value="" /> 
					<attribute name="FPARTNO" value="TESTASM1" /> 
					<attribute name="FGROUP" value="" /> 
					<attribute name="Stock Usage" value="" /> 
					<attribute name="Stock Description" value="" /> 
					<attribute name="Stock Number" value="" /> 
					<attribute name="FSOURCE" value="Make to Order" /> 
					<attribute name="FMEASURE" value="EA" /> 
					<attribute name="Configuration" value="Default" /> 
					<attribute name="Reference Count (BOM Quantity disregarded)" value="1" /> 
					<references>
						<document aliasset="ERP" id="testPart1" idattribute="Part Number" idcfgname="Default" pdmweid="9886">
							<configuration name="Default" quantity="1">
								<attribute name="FDESCRIPT" value="XXXPART1" /> 
								<attribute name="FSTDMEMO" value="" /> 
								<attribute name="FREV" value="X" /> 
								<attribute name="Vendor" value="" /> 
								<attribute name="FPRODCL" value="" /> 
								<attribute name="FPARTNO" value="testPart1" /> 
								<attribute name="FGROUP" value="" /> 
								<attribute name="Stock Usage" value="-" /> 
								<attribute name="Stock Description" value="" /> 
								<attribute name="Stock Number" value="" /> 
								<attribute name="FSOURCE" value="Make to Order" /> 
								<attribute name="FMEASURE" value="EA" /> 
								<attribute name="Configuration" value="Default" /> 
								<attribute name="Reference Count (BOM Quantity disregarded)" value="1" /> 
		  					</configuration>
  						</document>
 						<document aliasset="ERP" id="testPart2" idattribute="Part Number" idcfgname="Default" pdmweid="9887">
 							<configuration name="Default" quantity="1">
								<attribute name="FDESCRIPT" value="XXXPART2" /> 
								<attribute name="FSTDMEMO" value="" /> 
								<attribute name="FREV" value="X" /> 
								<attribute name="Vendor" value="" /> 
								<attribute name="FPRODCL" value="" /> 
								<attribute name="FPARTNO" value="testPart2" /> 
								<attribute name="FGROUP" value="" /> 
								<attribute name="Stock Usage" value="-" /> 
								<attribute name="Stock Description" value="" /> 
								<attribute name="Stock Number" value="" /> 
								<attribute name="FSOURCE" value="Make to Order" /> 
								<attribute name="FMEASURE" value="EA" /> 
								<attribute name="Configuration" value="Default" /> 
								<attribute name="Reference Count (BOM Quantity disregarded)" value="1" /> 
  							</configuration>
  						</document>
  					</references>
  				</configuration>
  			</document>
  		</transaction>
  	</transactions>
</xml>'

DECLARE @Doc integer

EXEC sp_xml_preparedocument @Doc OUTPUT, @Xml
-- INSERT TemporaryTable (FDESCRIPT, FSTDMEMO, FREV, Vendor, FPRODCL, FPARTNO, FGROUP, 
--                  StockUsage, StockDescription, StockNumber, FSOURCE, FMEASURE)
SELECT	FDESCRIPT,
	FSTDMEMO,
	FREV,
	Vendor,
	FPRODCL,
	FPARTNO,
	FGROUP,
	StockUsage,
	StockDescription,
	StockNumber,
	FSOURCE,
	FMEASURE
FROM	OPENXML (@Doc, 'xml/transactions/transaction/document/configuration', 1) WITH (
		FDESCRIPT varchar(20) 'attribute[@name="FDESCRIPT"]/@value',
		FSTDMEMO varchar(20) 'attribute[@name="FSTDMEMO"]/@value',
		FREV varchar(20) 'attribute[@name="FREV"]/@value',
		Vendor varchar(20) 'attribute[@name="Vendor"]/@value',
		FPRODCL varchar(20) 'attribute[@name="FPRODCL"]/@value',
		FPARTNO varchar(20) 'attribute[@name="FPARTNO"]/@value',
		FGROUP varchar(20) 'attribute[@name="FGROUP"]/@value',
		StockUsage varchar(20) 'attribute[@name="Stock Usage"]/@value',
		StockDescription varchar(20) 'attribute[@name="Stock Description"]/@value',
		StockNumber varchar(20) 'attribute[@name="Stock Number"]/@value',
		FSOURCE varchar(20) 'attribute[@name="FSOURCE"]/@value',
		FMEASURE varchar(20) 'attribute[@name="FMEASURE"]/@value'
		)

EXEC sp_xml_removedocument @Doc

Open in new window

0
 

Author Comment

by:SynsorIT
ID: 33673726
Solidoworks outputs the XML file as "partnumber".xml

Is there a way to modify this code to look at the xml files in a specific folder?

Example;
We have a folder y:/_BOMexport
1000146.xml
1000690.xml
1000710.xml
and so on

There could be hundreds of these a day.  Ideally we would want to read all files in the y:/_BOMexport folder and delete them after reading into sql.

Thank you!!!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33674702
For many reasons, but foremost security and performance, you are better off passing the Xml files using a front-end app written in VB or .NET rather then using MS SQL Server to do this.  That is not to say that you cannot do it using something like OPENROWSET() or (God forbid) the sp_OA* funtions, it is just that for your own sanity, don't do that, MS SQL Server was never intended for that purpose and using it as if it was MS Access is a very bad idea.
0
 

Author Comment

by:SynsorIT
ID: 33675058
So would best practice go something like this?

.net
- grab xml file from y:/_bomexport
- pass xml to temporary table in SQL (or should it pass the xml directly into the "item master" table)
- take xml data in SQL temp table and divide into columns and insert into "item master" table
- delete xml file from y:/bomexport
- repeat for all files in y:/bomexport


Thanks for your help, i am very new to this but am learning!!!
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 33678230
I would:
1. Load it with .NET
2. Pass it into SQL Server via a Stored Procedure.
3. Shred the Xml into a variable of type table (as opposed to a temporary table).
4. Validate the results in the variable of type table.
5. If it passes load into your "Item Master" table.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

856 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