How to import XML into SQL Server with the XML Bulk Load component (316005)



The information in this article applies to:

  • Microsoft SQL Server 2000 (all editions)
  • Microsoft SQL Server 2000 64 bit (all editions)
  • 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

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.

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

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="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 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>
				

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 "Create the XML Data Source File" heading into the table you created in the "Create Table to Receive the Data" heading by using the mapping schema discussed in the "Create 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
				
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 "Create the XML data source file" 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

REFERENCES

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

back to the top

Modification Type:MajorLast Reviewed:12/23/2005
Keywords:kbHOWTOmaster KB316005 kbAudDeveloper