SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Load XML into SQL server 2000 table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bpuccha
Starting Member

34 Posts

Posted - 06/24/2013 :  16:33:25  Show Profile  Reply with Quote
Hi,

I have to load the below XML into SQL server 2000 table.

create table temp
(
id int,
cycle_no varchar(36),
pdm varchar(1)
)

<?xml version="1.0" ?>
<batch>
<job id="100360001">
<cycle_no type="string">36</cycle_no>
<PDM type="string">C</PDM>
</job>
<job id="100360002">
<cycle_no type="string">36</cycle_no>
<PDM type="string">C</PDM>
</job>
</batch>

How can I load attribute value into temp table id column??
I tried using SQLXMLBulkLoad(by creating VB script),but it didn't insert the data into the table.



Edited by - bpuccha on 06/24/2013 16:37:58

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 06/25/2013 :  01:10:41  Show Profile  Reply with Quote
use OPENXML

http://msdn.microsoft.com/en-us/library/ms186918.aspx

something like

DECLARE @idoc int, @doc varchar(1000); 
SET @doc ='<batch>
<job id="100360001">
<cycle_no type="string">36</cycle_no> 
<PDM type="string">C</PDM> 
</job>
<job id="100360002">
<cycle_no type="string">36</cycle_no> 
<PDM type="string">C</PDM>
</job>
</batch>'; 

--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc; 


INSERT temp (id,cycle_no,pdm)
SELECT *
FROM   OPENXML (@idoc, '/batch/job',2) 
         WITH (ID       int         '@id', 
               cycle_no  varchar(36) './cycle_no', 
               pdm   varchar(1)    './PDM');

EXEC sp_xml_removedocument @idoc;


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bpuccha
Starting Member

34 Posts

Posted - 06/25/2013 :  10:51:07  Show Profile  Reply with Quote
Thanks for the solutions,It worked for me..What if my xml document is very large that I can't accoomdate in a varibale?


Go to Top of Page

bpuccha
Starting Member

34 Posts

Posted - 06/25/2013 :  11:08:57  Show Profile  Reply with Quote
I tried SQLXMLBulkLoad option by creating the mapping file and VB script, its not giving any error when I run the VB script and not loading the data into temp table.Here is my mapping file and VB script


Customers.xml :

<?xml version="1.0" ?>
<batch>
<job id="100360003">
<cycle_no type="string">36</cycle_no>
<PDM type="string">C</PDM>
</job>
<job id="100360004">
<cycle_no type="string">36</cycle_no>
<PDM type="string">C</PDM>
</job>
</batch>

Customermapping.xml :

<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:xml:datatypes"
xmlns:sql="urn:schemas-microsoft-com:xml-sql" >


<AttributeType name="Id" />
<ElementType name="Cycle_no" dt:type="string" />
<ElementType name="PDM" dt:type="string" />

<ElementType name="batch" sql:is-constant="1">
</ElementType>

<ElementType name="job" sql:relation="temp_xml">
<attribute type="Id" sql:field="Id" />
<element type="Cycle_no" sql:field="Cycle_no" />
<element type="PDM" sql:field="PDM" />
</ElementType>

</Schema>

Insertcustomers.vbs

Set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkLoad")
objBL.ConnectionString = "provider=XXXXXXXX;data source=XXXXXXXX;database=XXXXXX;uid=XXXXXX;pwd=XXXXX"
objBL.ErrorLogFile = "c:\error.log"
objBL.Execute "c:\customermapping.xml", "c:\customers.xml"
Set objBL = Nothing




Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 06/26/2013 :  03:16:06  Show Profile  Reply with Quote
quote:
Originally posted by bpuccha

Thanks for the solutions,It worked for me..What if my xml document is very large that I can't accoomdate in a varibale?





try using bcp
http://msdn.microsoft.com/en-us/library/ms162802.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000