Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

How To Import XML Into SQL Server With The XML Bulk Load Component

Uploaded by

Rui Felix
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

How To Import XML Into SQL Server With The XML Bulk Load Component

Uploaded by

Rui Felix
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

22-06-2010 How to import XML into SQL Server wit…

Article ID: 316005 - Last Review: December 23, 2005 - Revision: 4.4
How to import XML into SQL Server with the XML Bulk Load component
This article was previously published under Q316005

SUMMARY

Data that is expressed in XML can be loaded into a Microsoft SQL Server 2000
database by using the XML Bulk Load component. This article outlines the steps that you need to follow to load XML data
into a table that already exists in the database.

Note If you are using Microsoft SQL Server 2005, see the "XML Bulk Load Examples" topic in SQL Server 2005 Books
Online.

Requirements
To use the steps in this article you need:

Web Release 1 of XML for SQL Server 2000 (SQLXML), or later.

Prior knowledge required:

Knowledge of XML.

Create table to receive the data


Use the following steps to create a table to receive the data that the XML Bulk Load
component processes.

1. C reate a database named MyDatabase in SQL Server.


2. Open SQL Query Analyzer, and then change the database to MyDatabase.
3. C reate a Customer table in MyDatabase by running the following SQL statement in Query Analyzer:

USE MyDatabase CREATE TABLE Customer ( CustomerId INT PRIMARY KEY, CompanyName NVARCHAR(20), City
NVARCHAR(20))

Create the XML data source file


This is the sample data source code. Paste this XML into Notepad, and then save the
file as C :/C ustomers.xml.

<ROOT> <Customers> <CustomerId>1111</CustomerId> <CompanyName>Sean Chai</CompanyName> <City>NY</City>


</Customers> <Customers> <CustomerId>1112</CustomerId> <CompanyName>Tom Johnston</CompanyName> <City>LA</City>
</Customers> <Customers> <CustomerId>1113</CustomerId> <CompanyName>Institute of Art</CompanyName> </Customers>
</ROOT>

Create the mapping schema file


This next file is a file that you use to map the format of the data source XML to the
format of the Customer table in the database. Paste this XML into Notepad, and then save the file as
C :/C ustomermapping.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" > <ElementType name="CustomerId" dt:type="int"
/> <ElementType name="CompanyName" dt:type="string" /> <ElementType name="City" dt:type="string" /> <ElementType
http://support.microsoft.com/kb/316005 1/3
22-06-2010 How to import XML into SQL Server wit…
name="ROOT" sql:is-constant="1"> <element type="Customers" /> </ElementType> <ElementType name="Customers"
sql:relation="Customer"> <element type="CustomerId" sql:field="CustomerId" /> <element type="CompanyName"
sql:field="CompanyName" /> <element type="City" sql:field="City" /> </ElementType> </Schema>

Create a VBScript program to execute the XML Bulk Load component


This is the script that uses the XML Bulk Load component to insert the three records
you created in the "C reate the XML Data Source File" heading into the table you created in the "C reate Table to Receive
the Data" heading by using the mapping schema discussed in the "C reate the Mapping Schema File" heading. Paste this
VBScript code into Notepad, and then save the file as C :\Insertcustomers.vbs.

Set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkLoad") objBL.ConnectionString = "provider=SQLOLEDB.1;data


source=MySQLServer;database=MyDatabase;uid=MyAccount;pwd=MyPassword" objBL.ErrorLogFile = "c:\error.log"
objBL.Execute "c:\customermapping.xml", "c:\customers.xml" Set objBL = Nothing

C orrect the ConnectionString credentials on the second line of the code so that the script can work with your SQL
Server installation. If you do not correct line 2, the following error message occurs after you execute the script: Error
connecting to the data source

Run the VBScript program


Run the VBScript program C :\Insertcustomers.vbs to insert the three customer
records into the Customer table.

Verify it works
In SQL Query Analyzer, switch to the MyDatabase database, and then run this query:

SELECT * FROM Customer

Note that the three records created in the "C reate the XML data source file" heading are now in the Customer table.

Alternate technique
The XML Bulk Load component is capable of:

Mapping an XML document to multiples tables by using a relationship specified in the XML schema file.
Generating table schemas before bulk loading.
Bulk loading from a stream.
Bulk loading in overflow columns.

REFERENCES
SQLXML Books Online; topic: "Performing Bulk Load of XML Data"

APPLIES TO

Microsoft SQL Server 2000 Standard Edition


Microsoft SQL Server 2000 64-bit Edition
Microsoft SQL Server 2005 Standard Edition
Microsoft SQL Server 2005 Developer Edition
Microsoft SQL Server 2005 Enterprise Edition

http://support.microsoft.com/kb/316005 2/3
22-06-2010 How to import XML into SQL Server wit…
Microsoft SQL Server 2005 Express Edition
Microsoft SQL Server 2005 Workgroup Edition

Keywords: kbhowtomaster KB316005

Get Help Now


C ontact a support professional by E-mail, Online, or Phone

Microsoft Support
©2010 Microsoft

http://support.microsoft.com/kb/316005 3/3

You might also like