Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum Rss Feed

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
SQL Scripts & Samples
» Database Forum
» Slideshows
Free Newsletters:

News Via RSS Feed

Rss Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles


Posted Aug 12, 2003

XML and SQL 2000 (Part 8)

By Marcin Policht

So far, this series has concentrated on configuration options available via the graphical interface of IIS Virtual Directory Management for SQL Server. However, newer versions of SQLXML, in addition to the features we have already covered, provide another type of functionality, accessible through scripting, that allows bulk loading of XML data into SQL databases. Such functionality will be the topic of this article. For this, you should install the most recent release of SQLXML (version 3.0 currently at Service Pack 1 level, available for download from the Microsoft Web Site).

Mechanisms for modifying SQL databases using XML formatted data, such as the previously described updategrams, are not suitable for large amounts of data, because they require the entire XML document to be loaded into memory prior to initiating the insert operation. Bulk loading of XML data (equivalent to bulk loading using standard SQL server methods as Bulk Copy Program or BULK INSERT) provides a more efficient method of dealing with such situations.

XML-based bulk load operation is implemented as the XML Bulk Load COM component. You can take advantage of this component through use of programming (including .NET applications) or scripting (e.g. via VBScript, which will be the method presented here). The XML Bulk Load component uses data in XML format stored in an XML document (or an XML fragment) and an annotated mapped schema in XDR (XML Data Reduced) or XSD (XML Schema Definition) format (which provides XML data description and verification).

As you might recall from our earlier discussions, XDR schema has been supported (as the de-facto standard) since the release of SQL Server 2000 (and accompanying SQLXML 1.0). Once the XML Schema Definition standard had been ratified (and recommended) by the World Wide Web Consortium, Microsoft included it in subsequent releases of SQLXML (2.0 and 3.0). If you intend to work with XSD schemas, you can convert XDR schema examples presented in this article to XSD format using CVTSCHEMA.EXE included with SQLXML 3.0 (you can find this file in Program Files\SQLXML 3.0\bin folder).

To better understand the bulk load mechanism, let's take a look at a fairly simple sample of code that inserts XML-formatted data into the Shippers table of the Northwind database.

Set oXMLBulkLoad = CreateObject("SQLXMLBulkLoad.SQLXMLBulkLoad")
oXMLBulkLoad.ConnectionString = "PROVIDER=SQLOLEDB;SERVER=YourSQLServer;" & _
oXMLBulkLoad.Execute "C:\XMLData\Shippers.xdr", "C:\XMLData\Shippers.xml"
Set oXMLBulkLoad = Nothing

As you can see, first we instantiate an object (in our example, called oXMLBulkLoad) of type SQLXMLBulkLoad - this class is represented in the HKEY_CLASSES_ROOT portion of the registry by SQLXMLBulkLoad.SQLXMLBulkLoad entry (referred to as ProgID), hence this is the parameter used by the CreateObject method. Next, we specify the connection string property of the object we just created. The connection string contains the provider name (SQLOLEDB), server name (make sure you replace the value "YourSQLServer" with the name of your SQL server), database name (Northwind in this case) and type of authentication used for the connection (Windows integrated). The actual bulk insert takes place when the Execute method of the oXMLBulkLoad object is invoked. The method takes two parameters - full paths of the files containing a schema - in XDR or XSD format - and corresponding XML document, respectively. If you save the script as BulkLoadShippers.vbs, you could execute it by running

cscript BulkLoadShippers.vbs

at the Command Prompt or simply double-clicking the file from Windows Explorer.

However, before you do this, you need to ensure that you have properly formatted schema and XML-document files, available in the locations specified in the script. Our XML document will have the following format:

<?xml version="1.0" encoding="utf-8" ?> 
    <Shipper ShipperID="4" CompanyName="Speedy Gonzalez" Phone="(503) 555-9934" /> 
    <Shipper ShipperID="5" CompanyName="The RoadRunner" Phone="(503) 555-3456" /> 

which corresponds to the following XDR schema:

<Schema xmlns="urn:schemas-microsoft-com:xml-data"

	<ElementType name="Shipper" sql:relation="Shippers">
		<AttributeType name="ShipperID" required="yes" dt:type="int"/>
		<AttributeType name="CompanyName" required="yes" dt:type="string"/>
		<AttributeType name="Phone" required="no" dt:type="string"/>

		<attribute type="ShipperID"/>
		<attribute type="CompanyName"/>
		<attribute type="Phone"/>

MS SQL Archives

Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM