Solved

Import XML file into SQL 2000

Posted on 2010-09-13
5
686 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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.
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

776 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