Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Load XML into SQL server 2000 table

Author  Topic 

bpuccha
Starting Member

34 Posts

Posted - 2013-06-24 : 16:33:25
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.


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-25 : 01:10:41
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 - 2013-06-25 : 10:51:07
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 - 2013-06-25 : 11:08:57
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

52326 Posts

Posted - 2013-06-26 : 03:16:06
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
   

- Advertisement -