Microsoft KB Archive/316005

= How to import XML into SQL Server with the XML Bulk Load component =

Article ID: 316005

Article Last Modified on 12/23/2005

-

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
 * Microsoft SQL Server 2005 Express Edition
 * Microsoft SQL Server 2005 Workgroup Edition

-



This article was previously published under Q316005





IN THIS TASK
SUMMARY
 * Requirements
 * Create table to receive the data
 * Create the XML data source file
 * Create the mapping schema file
 * Create a VBScript program to execute the XML Bulk Load component
 * Run the VBScript program
 * Verify it works
 * Alternate technique

REFERENCES



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 &quot;XML Bulk Load Examples&quot; topic in SQL Server 2005 Books Online.

back to the top

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.

back to the top

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.  Create a database named MyDatabase in SQL Server. Open SQL Query Analyzer, and then change the database to MyDatabase.  Create 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)) 

back to the top

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:/Customers.xml.   1111 <CompanyName>Sean Chai</CompanyName> <City>NY</City> </Customers> <Customers> 1112</CustomerId> <CompanyName>Tom Johnston</CompanyName> <City>LA</City> </Customers> <Customers> 1113</CustomerId> <CompanyName>Institute of Art</CompanyName> </Customers> </ROOT>

back to the top

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:/Customermapping.xml. <?xml version=&quot;1.0&quot; ?> <Schema xmlns=&quot;urn:schemas-microsoft-com:xml-data&quot; xmlns:dt=&quot;urn:schemas-microsoft-com:xml:datatypes&quot; xmlns:sql=&quot;urn:schemas-microsoft-com:xml-sql&quot; >

<ElementType name=&quot;CustomerId&quot; dt:type=&quot;int&quot; /> <ElementType name=&quot;CompanyName&quot; dt:type=&quot;string&quot; /> <ElementType name=&quot;City&quot; dt:type=&quot;string&quot; />

<ElementType name=&quot;ROOT&quot; sql:is-constant=&quot;1&quot;> <element type=&quot;Customers&quot; /> </ElementType>

<ElementType name=&quot;Customers&quot; sql:relation=&quot;Customer&quot;> <element type=&quot;CustomerId&quot; sql:field=&quot;CustomerId&quot; /> <element type=&quot;CompanyName&quot; sql:field=&quot;CompanyName&quot; /> <element type=&quot;City&quot;       sql:field=&quot;City&quot; /> </ElementType>

</Schema>

back to the top

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 &quot;Create the XML Data Source File&quot; heading into the table you created in the &quot;Create Table to Receive the Data&quot; heading by using the mapping schema discussed in the &quot;Create the Mapping Schema File&quot; heading. Paste this VBScript code into Notepad, and then save the file as C:\Insertcustomers.vbs. Set objBL = CreateObject(&quot;SQLXMLBulkLoad.SQLXMLBulkLoad&quot;) objBL.ConnectionString = &quot;provider=SQLOLEDB.1;data source=MySQLServer;database=MyDatabase;uid=MyAccount;pwd=MyPassword&quot; objBL.ErrorLogFile = &quot;c:\error.log&quot; objBL.Execute &quot;c:\customermapping.xml&quot;, &quot;c:\customers.xml&quot; Set objBL = Nothing Correct 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

back to the top

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

back to the top

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 &quot;Create the XML data source file&quot; heading are now in the Customer table.

back to the top

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.

back to the top