Export (0) Print
Expand All

Examples of Bulk Loading XML Documents

SQL Server 2000

The following examples illustrate the XML Bulk Load functionality in Microsoft® SQL Server™ 2000. Each example provides an XSD schema and its equivalent XDR schema.

A. Bulk loading XML in a table

The following script, written in the Microsoft Visual Basic® Scripting Edition (VBScript), loads an XML document into a table.

set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.3.0")
objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=Northwind;integrated security=SSPI"
objBL.ErrorLogFile = "c:\error.log"
objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"
set objBL=Nothing

This example establishes a connection to the instance of SQL Server that is specified in the ConnectionString property. The example also specifies the ErrorLogFile property. Therefore, the error output is saved in the specified file. Notice also that the Execute method has as its parameters both the mapping schema file and the XML data file.

To test a sample bulk load

  1. Create this table:
    CREATE TABLE Cust(CustomerID  int PRIMARY KEY,
                           CompanyName varchar(20),
                      City        varchar(20))
    GO
    
  2. Create a file in Notepad, and save it as SampleSchema.xml. To this file, add the following XSD schema:
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
    
      <xsd:element name="Customers" sql:relation="Cust" >
       <xsd:complexType>
         <xsd:sequence>
           <xsd:element name="CustomerID"  type="xsd:integer" />
           <xsd:element name="CompanyName" type="xsd:string" />
           <xsd:element name="City"        type="xsd:string" />
         </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    
  3. Create a file in Notepad, and save it as SampleXMLData.xml. To this file, add the following XML document:
    <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>
    
  4. Create a file in Notepad, and save it as BLoad.vbs. To this file, add the VBScript code that is provided in this example. Modify the connection string to provide the appropriate server and database name. Specify the appropriate path for the files that are specified as parameters to the Execute method.

  5. Execute the VBScript code. XML Bulk Load loads the XML into the Cust table.

This is the equivalent XDR schema:

<?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="Cust" >
      <element type="CustomerID"  sql:field="CustomerID" />
      <element type="CompanyName" sql:field="CompanyName" />
      <element type="City"        sql:field="City" />

   </ElementType>
</Schema>
B. Bulk loading XML data in multiple tables

In this example, the XML document consists of the <Customer> and <Order> elements.

<ROOT>
  <Customers>
    <CustomerID>1111</CustomerID>
    <CompanyName>Sean Chai</CompanyName>
    <City>NY</City>
    <Order OrderID="1" />
    <Order OrderID="2" />
  </Customers>
  <Customers>
    <CustomerID>1112</CustomerID>
    <CompanyName>Tom Johnston</CompanyName>
     <City>LA</City>  
    <Order OrderID="3" />
  </Customers>
  <Customers>
    <CustomerID>1113</CustomerID>
    <CompanyName>Institute of Art</CompanyName>
    <Order OrderID="4" />
  </Customers>
</ROOT>

This following Microsoft Visual Basic Scripting Edition (VBScript) script bulk loads the XML data into two tables, Cust and CustOrder:

Cust(CustomerID, CompanyName, City)
CustOrder(OrderID, CustomerID)

The following XSD schema defines the XML view of these tables. The schema specifies the parent-child relationship between the <Customer> and <Order> elements.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:annotation>
  <xsd:appinfo>
    <sql:relationship name="CustCustOrder"
          parent="Cust"
          parent-key="CustomerID"
          child="CustOrder"
          child-key="CustomerID" />
  </xsd:appinfo>
</xsd:annotation>

  <xsd:element name="Customers" sql:relation="Cust" >
   <xsd:complexType>
     <xsd:sequence>
       <xsd:element name="CustomerID"  type="xsd:integer" />
       <xsd:element name="CompanyName" type="xsd:string" />
       <xsd:element name="City"        type="xsd:string" />
       <xsd:element name="Order" 
                          sql:relation="CustOrder"
                          sql:relationship="CustCustOrder" >
         <xsd:complexType>
          <xsd:attribute name="OrderID" type="xsd:integer" />
         </xsd:complexType>
       </xsd:element>
     </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

</xsd:schema>

The XML bulk load in the following VBScript uses the primary key/foreign key relationship that is specified between the <Cust> and <CustOrder> elements to bulk load the data into the tables.

set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.3.0")
objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=Northwind;integrated security=SSPI"
objBL.ErrorLogFile = "c:\error.log"
objBL.Execute "SampleSchema.xml", "SampleData.xml"
set objBL=Nothing

To test a sample bulk load

  1. Create two tables:
    CREATE TABLE Cust(
           CustomerID  int PRIMARY KEY,
           CompanyName varchar(20),
           City        varchar(20))
    
    CREATE TABLE CustOrder(        OrderID     int PRIMARY KEY, 
            CustomerID int FOREIGN KEY REFERENCES Cust(CustomerID))
    
  2. Create a file in Notepad, and save it as SampleSchema.xml. Add the XSD schema that is provided in this example to the file.

  3. Create a file in Notepad, and save it as SampleXMLData.xml. Add the XML document that was provided in the earlier example, "Bulk loading XML in a table," to the file.

  4. Create a file in Notepad, and save it as BLoad.vbs. To this file, add the VBScript code provided in this example. Modify the connection string to provide the appropriate server and database name. Specify the appropriate path for the files that are specified as parameters to the Execute method.

  5. Execute the VBScript code. XML Bulk Load loads the XML document into the Cust table.

This is the equivalent XDR schema:

<?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="Cust" >
      <element type="CustomerID" sql:field="CustomerID" />
      <element type="CompanyName" sql:field="CompanyName" />
      <element type="City" sql:field="City" />
      <element type="Order" >
<sql:relationship
                key-relation="Cust"
                key="CustomerID"
                foreign-key="CustomerID"
                foreign-relation="CustOrder" />
      </element>
   </ElementType>
    <ElementType name="Order" sql:relation="CustOrder" >
      <AttributeType name="OrderID" />
      <AttributeType name="CustomerID" />
      <attribute type="OrderID" />
      <attribute type="CustomerID" />
    </ElementType>
</Schema>
C. Using chain relationships in the schema to bulk load XML

This example illustrates how the M:N relationship that is specified in the mapping schema is used by XML Bulk Load to load data in a table that represents an M:N relationship.

For example, consider this XSD schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:annotation>
  <xsd:appinfo>
    <sql:relationship name="OrderOD"
          parent="Ord"
          parent-key="OrderID"
          child="OrderDetail"
          child-key="OrderID" />

    <sql:relationship name="ODProduct"
          parent="OrderDetail"
          parent-key="ProductID"
          child="Product"
          child-key="ProductID" 
          inverse="true"/>
  </xsd:appinfo>
</xsd:annotation>

  <xsd:element name="Order" sql:relation="Ord" 
                            sql:key-fields="OrderID" >
   <xsd:complexType>
     <xsd:sequence>
        <xsd:element name="Product" sql:relation="Product" 
                     sql:key-fields="ProductID"
                     sql:relationship="OrderOD ODProduct">
          <xsd:complexType>
             <xsd:attribute name="ProductID" type="xsd:int" />
             <xsd:attribute name="ProductName" type="xsd:string" />
          </xsd:complexType>
        </xsd:element>
     </xsd:sequence>
        <xsd:attribute name="OrderID"   type="xsd:integer" /> 
        <xsd:attribute name="CustomerID"   type="xsd:string" />
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

The schema specifies <Order> element with <Product> child element. The <Order> element maps to Ord table and the <Product> element maps to the Product table in the database. The chain-relationship specified on the <Product> element identifies a M:N relationship represented by the OrderDetail table. (An order may include many products, and a product may be included in many orders.)

When you are bulk loading an XML document with this schema, records are added to the Ord, Product, and OrderDetail tables.

To test a working sample

  1. Create three tables:
    CREATE TABLE Ord (
             OrderID     int  PRIMARY KEY,
             CustomerID  varchar(5))
    GO
    CREATE TABLE Product (
             ProductID   int PRIMARY KEY,
             ProductName varchar(20))
    GO
    CREATE TABLE OrderDetail (
           OrderID     int FOREIGN KEY REFERENCES Ord(OrderID),
           ProductID   int FOREIGN KEY REFERENCES Product(ProductID),
                       CONSTRAINT OD_key PRIMARY KEY (OrderID, ProductID))
    GO
    
  2. Save the schema that is provided in this example (SampleSchema.xml).

  3. Save the sample XML data (SampleXMLData.xml) that is provided in the earlier example, "Bulk loading XML in a table":
    <ROOT>  
      <Order OrderID="1" CustomerID="ALFKI">
        <Product ProductID="1" ProductName="Chai" />
        <Product ProductID="2" ProductName="Chang" />
      </Order>
      <Order OrderID="2" CustomerID="ANATR">
         <Product ProductID="3" ProductName="Aniseed Syrup" />
        <Product ProductID="4" ProductName="Gumbo Mix" />
      </Order>
    </ROOT>
    
  4. To execute XML Bulk Load, save and execute the following Microsoft Visual Basic Scripting Edition (VBScript) example as Sample.vbs. You must edit the code and specify the appropriate directory path for the schema file and sample data file.
    set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.3.0")
    objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=Northwind;integrated security=SSPI"
    objBL.ErrorLogFile = "c:\error.log"
    objBL.CheckConstraints = True
    objBL.Transaction=True
    objBL.Execute "c:\...\SampleSchema.xml", "c:\...\SampleXMLData.xml"
    set objBL=Nothing
    
D. Bulk loading in identity type columns

This example illustrates how bulk load handles identity type columns. In the example, data is bulk loaded into three tables (Ord, Product, and OrderDetail).

In these tables,

  • OrderID in the Ord table is an identity type column

  • ProductID in the Product table is an identity type column.

  • OrderID and ProductID columns in the OrderDetail are foreign key columns referring to corresponding primary key columns in the Ord and Product tables.

The following are the table schemas for this example:

Ord (OrderID, CustomerID)

Product (ProductID, ProductName)

OrderDetail (OrderID, ProductID)

In this example of XML bulk load, the KeepIdentity property of the BulkLoad object model is set to false. Therefore, SQL Server generates identity values for the ProductID and OrderID columns in the Product and Ord tables respectively (any values provided in the documents to be bulk loaded are ignored).

In this case, bulk load identifies the primary key/foreign key relationship among tables and first inserts records in the tables with the primary key, and then propagates the identity value generated by SQL Server to the tables with foreign key columns. In the following example, bulk load inserts data in tables in this order:

  1. Product

  2. Ord

  3. OrderDetail

Note: In order to propagate identity values generated in the Products and Orders tables, the processing logic requires Bulkload to keep track of these values for later insertion into the OrderDetails table. In order to do that, XML BulkLoad creates intermediate tables, populates the data in these tables, and later removes them.

To test a working sample

  1. Create these tables:
    CREATE TABLE Ord (
             OrderID     int identity(1,1)  PRIMARY KEY,
             CustomerID  varchar(5))
    GO
    CREATE TABLE Product (
             ProductID   int identity(1,1) PRIMARY KEY,
             ProductName varchar(20))
    GO
    CREATE TABLE OrderDetail (
           OrderID     int FOREIGN KEY REFERENCES Ord(OrderID),
           ProductID   int FOREIGN KEY REFERENCES Product(ProductID),
                       CONSTRAINT OD_key PRIMARY KEY (OrderID, ProductID))
    GO
  2. Create a file in Notepad, and save it as SampleSchema.xml. Add this XSD schema to this file.
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
    <xsd:annotation>
      <xsd:appinfo>
        <sql:relationship name="OrderOD"
              parent="Ord"
              parent-key="OrderID"
              child="OrderDetail"
              child-key="OrderID" />
    
        <sql:relationship name="ODProduct"
              parent="OrderDetail"
              parent-key="ProductID"
              child="Product"
              child-key="ProductID" 
              inverse="true"/>
      </xsd:appinfo>
    </xsd:annotation>
    
      <xsd:element name="Order" sql:relation="Ord" 
                                sql:key-fields="OrderID" >
       <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="Product" sql:relation="Product" 
                         sql:key-fields="ProductID"
                         sql:relationship="OrderOD ODProduct">
              <xsd:complexType>
                 <xsd:attribute name="ProductID" type="xsd:int" />
                 <xsd:attribute name="ProductName" type="xsd:string" />
              </xsd:complexType>
            </xsd:element>
         </xsd:sequence>
            <xsd:attribute name="OrderID"   type="xsd:integer" /> 
            <xsd:attribute name="CustomerID"   type="xsd:string" />
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  3. Create a file in Notepad, and save it as SampleXMLData.xml. Add the following XML document.
    <ROOT>  
      <Order OrderID="11" CustomerID="ALFKI">
        <Product ProductID="11" ProductName="Chai" />
        <Product ProductID="22" ProductName="Chang" />
      </Order>
      <Order OrderID="22" CustomerID="ANATR">
         <Product ProductID="33" ProductName="Aniseed Syrup" />
        <Product ProductID="44" ProductName="Gumbo Mix" />
      </Order>
    </ROOT>
  4. Create a file in Notepad, and save it as BLoad.vbs. To this file, add the following  VBScript code. Modify the connection string to provide the appropriate server and database name. Specify the appropriate path for the files that serve as parameters to the Execute method.
    set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.3.0")
    objBL.ConnectionString = "provider=SQLOLEDB;data source=server;database=Database;integrated security=SSPI"
    objBL.ErrorLogFile = "C:\dlls\blkld30sp2\error.xml"
    objBL.CheckConstraints = True
    objBL.Transaction=False
    objBL.KeepIdentity=False
    objBL.Execute "c:\...\SampleSchema.xml", "c:\...SampleXMLData.xml"
    set objBL=Nothing
    
    msgbox "Done."
  5. Execute the VBScript code. The XML Bulk Load will load the data into the appropriate tables.
E. Generating table schemas before bulk loading

XML Bulk Load can optionally generate the tables if they do not exist before bulk loading. Setting the SchemaGen property of the SQLXMLBulkLoad object to TRUE does this. You can also optionally request XML Bulk Load to drop any existing tables and re-create them by setting the SGDropTables property to TRUE. The following Microsoft Visual Basic Scripting Edition (VBScript) example illustrates the use of these properties.

Also, this example sets two additional properties to TRUE:

  • CheckConstraints. Setting this property to TRUE ensures that the data being inserted into the tables does not violate any constraints that have been specified on the tables (in this case the PRIMARY KEY/FOREIGN KEY constraints specified between the Cust and CustOrder tables). If there is a constraint violation, the bulk load fails.

  • XMLFragment. This property must be set to TRUE because the sample XML document (data source) contains no single, top-level element (and thus, is a fragment).

This is the VBScript code:

Dim objBL 
Set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.3.0")
objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=Northwind;integrated security=SSPI"
objBL.ErrorLogFile = "c:\error.log"

objBL.CheckConstraints=true
objBL.XMLFragment = True
objBL.SchemaGen = True
objBL.SGDropTables = True

objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"
Set objBL = Nothing

To test a working sample

  1. Create a file in Notepad, and save it as SampleSchema.xml. Add the XSD schema that is provided in the earlier example, "Using chain relationships in the schema to bulk load XML," to the file.

  2. Create a file in Notepad, and save it as SampleXMLData.xml. Add the XML document that is provided in the earlier example, "Using chain relationships in the schema to bulk load XML," to the file. Remove the <ROOT> element from the document (to make it a fragment).

  3. Create a file in Notepad, and save it as BLoad.vbs. To this file, add the VBScript code in this example. Modify the connection string to provide the appropriate server and database name. Specify the appropriate path for the files that are specified as parameters to the Execute method.

  4. Execute the VBScript code. The XML Bulk Load creates the necessary tables on the basis of the mapping schema that is provided and bulk loads the data in it.
F. Bulk loading from a stream

The Execute method of the XML Bulk Load object model takes two parameters. The first parameter is the mapping schema file. The second parameter provides the XML data that is to be loaded in the database. There are two ways to pass the XML data to the Execute method of XML Bulk Load:

  • Specify the file name as the parameter.

  • Pass a stream that contains the XML data.

This example illustrates how to bulk load from a stream.

Microsoft Visual Basic Scripting Edition (VBScript) first executes a SELECT statement to retrieve customer information from the Customers table in the Northwind database. Because the FOR XML clause is specified (with the ELEMENTS option) in the SELECT statement, the query returns an element-centric XML document of this form:

<Customer>
  <CustomerID>..</CustomerID>
  <CompanyName>..</CompanyName>
  <City>..</City>
</Customer>
...

The script then passes the XML as a stream to the Execute method as its second parameter. The Execute method bulk loads the data into the Cust table.

Because this script sets the SchemaGen property to TRUE and SGDropTables property to TRUE, XML Bulk Load creates the Cust table (and if the table already exists, it first drops the table and then re-creates it) in the specified database.

This is the VBScript example:

Set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.3.0")
Set objCmd = CreateObject("ADODB.Command")
Set objConn = CreateObject("ADODB.Connection")
Set objStrmOut = CreateObject ("ADODB.Stream")

objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=Northwind;integrated security=SSPI"
objBL.ErrorLogFile     = "c:\error.log"
objBL.CheckConstraints = True
objBL.SchemaGen        = True
objBL.SGDropTables     = True
objBL.XMLFragment      = True
' Open a connection to the instance of SQL Server to get the source data.

objConn.Open "provider=SQLOLEDB;server=(local); database=Northwind; uid=UserName;pwd=Password"
Set objCmd.ActiveConnection = objConn
objCmd.CommandText = "SELECT CustomerID, CompanyName, City FROM Customers FOR XML AUTO, ELEMENTS"

' Open the return stream and execute the command.
Const adCRLF = -1
Const adExecuteStream = 1024
objStrmOut.Open
objStrmOut.LineSeparator = adCRLF
objCmd.Properties("Output Stream").Value = objStrmOut
objCmd.Execute , , adExecuteStream
objStrmOut.Position = 0

' Execute bulk load. Read source XML data from the stream.
objBL.Execute "SampleSchema.xml", objStrmOut

Set objBL = Nothing

The following XSD mapping schema provides the necessary information to create the table:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:element name="ROOT" sql:is-constant="true" >
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element ref="Customers"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:element name="Customers" sql:relation="Cust" >
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="CustomerID"  
                   type="xsd:string" 
                   sql:datatype="nvarchar(5)"/>
      <xsd:element name="CompanyName" 
                   type="xsd:string" 
                   sql:datatype="nvarchar(40)"/>
      <xsd:element name="City"  
                   type="xsd:string" 
                   sql:datatype="nvarchar(40)"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
</xsd:schema>

This is equivalent XDR schema:

<?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="Cust"  >
      <element type="CustomerID" sql:field="CustomerID" />
      <element type="CompanyName" sql:field="CompanyName" />
      <element type="City" sql:field="City" />
    </ElementType>
</Schema>
Opening a Stream on an Existing File

You can also open a stream on an existing XML data file and pass the stream as a parameter to the Execute method (instead of passing the file name as the parameter).

This is a Visual Basic example of passing a stream as the parameter:

Private Sub Form_Load()
Dim objBL As New SQLXMLBulkLoad
Dim objStrm As New ADODB.Stream
Dim objFileSystem As New Scripting.FileSystemObject
Dim objFile As Scripting.TextStream

MsgBox "Begin BulkLoad..."
objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=Northwind;integrated security=SSPI"
objBL.ErrorLogFile = "c:\error.log"
objBL.CheckConstraints = True
objBL.SchemaGen = True
objBL.SGDropTables = True
' Here again a stream is specified that contains the source data 
' (instead of the file name). But this is just an illustration.
' Usually this is useful if you have an XML data 
' stream that is created by some other means that you want to bulk 
' load. This example starts with an XML text file, so it may not be the 
' best to use a stream (you can specify the file name directly).
' Here you could have specified the file name itself. 
Set objFile = objFileSystem.OpenTextFile("c:\SampleData.xml")
objStrm.Open
objStrm.WriteText objFile.ReadAll
objStrm.Position = 0
objBL.Execute "c:\SampleSchema.xml", objStrm

Set objBL = Nothing
MsgBox "Done."
End Sub

To test the application, use the following XML document in a file (SampleData.xml) and the XSD schema that is provided in this example:

This is the XML source data (SampleData.xml):

<ROOT>
  <Customers>
    <CustomerID>1111</CustomerID>
    <CompanyName>Hanari Carnes</CompanyName>
    <City>NY</City>
    <Order OrderID="1" />
    <Order OrderID="2" />
  </Customers>

  <Customers>
    <CustomerID>1112</CustomerID>
    <CompanyName>Toms Spezialitten</CompanyName>
     <City>LA</City>
    <Order OrderID="3" />
  </Customers>
  <Customers>
    <CustomerID>1113</CustomerID>
    <CompanyName>Victuailles en stock</CompanyName>
    <Order CustomerID= "4444" OrderID="4" />
</Customers>
</ROOT>

This is the equivalent XDR schema:

<?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="Order" sql:relation="CustOrder" >
      <AttributeType name="OrderID" />
      <AttributeType name="CustomerID" />
      <attribute type="OrderID" />
      <attribute type="CustomerID" />
    </ElementType>

   <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="Cust"  >
      <element type="CustomerID" sql:field="CustomerID" />
      <element type="CompanyName" sql:field="CompanyName" />
      <element type="City" sql:field="City" />
      <element type="Order" >
             <sql:relationship
                key-relation="Cust"
                key="CustomerID"
                foreign-key="CustomerID"
                foreign-relation="CustOrder" />
      </element>
   </ElementType>
</Schema>
G. Bulk loading in overflow columns

If the mapping schema specifies an overflow column by using the sql:overflow-field annotation, XML Bulk Load copies all unconsumed data from the source document into this column.

Consider this XSD schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:annotation>
  <xsd:appinfo>
    <sql:relationship name="CustCustOrder"
          parent="Cust"
          parent-key="CustomerID"
          child="CustOrder"
          child-key="CustomerID" />
  </xsd:appinfo>
</xsd:annotation>
  <xsd:element name="Customers" sql:relation="Cust"
                                sql:overflow-field="OverflowColumn" >
   <xsd:complexType>
     <xsd:sequence>
       <xsd:element name="CustomerID"  type="xsd:integer" />
       <xsd:element name="CompanyName" type="xsd:string" />
       <xsd:element name="City"        type="xsd:string" />
       <xsd:element name="Order" 
                          sql:relation="CustOrder"
                          sql:relationship="CustCustOrder" >
         <xsd:complexType>
          <xsd:attribute name="OrderID" type="xsd:integer" />
          <xsd:attribute name="CustomerID" type="xsd:integer" />
         </xsd:complexType>
       </xsd:element>
     </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

The schema identifies an overflow column (OverflowColumn) for the Cust table. As a result, all unconsumed XML data for each <Customer> element is added to this column.

All abstract elements (elements for which abstract="true" is specified) and all prohibited attributes (attributes for which prohibited="true" is specified) are considered overflow by XML Bulk Load and are added to the overflow column, if specified. (Otherwise, they are ignored.)

To test a working sample

  1. Create two tables:
    CREATE TABLE Cust (
                  CustomerID     int         PRIMARY KEY,
                  CompanyName    varchar(20) NOT NULL,
                  City           varchar(20) DEFAULT 'Seattle',
                  OverflowColumn nvarchar(200))
    GO
    CREATE TABLE CustOrder (
                  OrderID    int PRIMARY KEY,
                  CustomerID int FOREIGN KEY 
                                 REFERENCES Cust(CustomerID))
    GO
    
  2. Create a file in Notepad, and save it as SampleSchema.xml. Add the XSD schema that is provided in this example to the file.

  3. Create a file in Notepad, and save it as SampleXMLData.xml. Add the following XML document to the file:
    <ROOT>
      <Customers>
        <CustomerID>1111</CustomerID>
        <CompanyName>Hanari Carnes</CompanyName>
        <City><![CDATA[NY]]> </City>
        <Junk>garbage in overflow</Junk>
        <Order OrderID="1" />
        <Order OrderID="2" />
      </Customers>
    
      <Customers>
        <CustomerID>1112</CustomerID>
        <CompanyName>Toms Spezialitten</CompanyName>
         <![CDATA[LA]]> 
        <!-- <xyz><address>111 Maple, Seattle</address></xyz>   -->
        <Order OrderID="3" />
      </Customers>
      <Customers>
        <CustomerID>1113</CustomerID>
        <CompanyName>Victuailles en stock</CompanyName>
        <Order OrderID="4" />
    </Customers>
    </ROOT>
    
  4. Create a file in Notepad, and save it as BLoad.vbs. To this file, add the following Microsoft Visual Basic Scripting Edition (VBScript) code. Modify the connection string to provide the appropriate server and database name. Specify the appropriate path for the files that are specified as parameters to the Execute method.
    set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.3.0")
    objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=Northwind;integrated security=SSPI"
    objBL.ErrorLogFile = "c:\error.log"
    objBL.CheckConstraints = True
    objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"
    set objBL=Nothing
    
  5. Execute the VBScript code.

This is the equivalent XDR schema:

<?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="Order" sql:relation="CustOrder" >
      <AttributeType name="OrderID" />
      <AttributeType name="CustomerID" />
      <attribute type="OrderID" />
      <attribute type="CustomerID" />
    </ElementType>

   <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="Cust" 
                       sql:overflow-field="OverflowColumn"  >
      <element type="CustomerID" sql:field="CustomerID" />
      <element type="CompanyName" sql:field="CompanyName" />
      <element type="City" sql:field="City" />
      <element type="Order" >
             <sql:relationship
                key-relation="Cust"
                key="CustomerID"
                foreign-key="CustomerID"
                foreign-relation="CustOrder" />
      </element>
   </ElementType>
</Schema>
H. Specifying the file path for temp files in transaction mode

When you are bulk loading in transaction mode (that is, when the Transaction property is set to TRUE), you also must set the TempFilePath property when either of these conditions exist:

  • You are bulk loading to a remote server.

  • You want to use an alternate local drive or folder (one other than the path that is specified by the TEMP environment variable) to store the temporary files that are created in the transaction mode.

For example, the following Microsoft Visual Basic Scripting Edition (VBScript) code bulk loads data from the SampleXMLData.xml file into the database tables in transaction mode. The TempFilePath property is specified to set the path for the temporary files that are generated in transaction mode.

set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.3.0")
objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=Northwind;integrated security=SSPI"
objBL.ErrorLogFile = "c:\error.log"
objBL.CheckConstraints = True
objBL.Transaction=True
objBL.TempFilePath="\\Server\MyDir"
objBL.Execute "c:\SampleSchema.xml", "c:\SampleXMLData.xml"
set objBL=Nothing

The temporary file path must be a shared location that is accessible to the service account of the target instance of Microsoft SQL Server™ and to the account that is running the bulk load application. Unless you are bulk loading on a local server, the temporary file path must be a UNC path (such as \\servername\sharename).

To test a working sample

  1. Create this table:
    CREATE TABLE Cust (
         CustomerID uniqueidentifier, 
          LastName  varchar(20))
    GO
    
  2. Create a file in Notepad, and save it as SampleSchema.xml. Add the following XSD schema to the file:
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
      <xsd:element name="ROOT" sql:is-constant="true" >
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element ref="Customers" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    
      <xsd:element name="Customers" sql:relation="Cust" >
       <xsd:complexType>
         <xsd:attribute name="CustomerID"  type="xsd:string" />
         <xsd:attribute name="LastName" type="xsd:string" />
       </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    
  3. Create a file in Notepad, and save it as SampleXMLData.xml. Add the following XML document to the file:
    <ROOT>
    <Customers CustomerID="6F9619FF-8B86-D011-B42D-00C04FC964FF" 
               LastName="Smith" />
    </ROOT>
    
  4. Create a file in Notepad, and save it as BLoad.vbs. To this file, add the following VBScript code. Modify the connection string to provide the appropriate server and database name. Specify the appropriate path for the files that are specified as parameters to the Execute method. Also specify the appropriate path for the TempFilePath property.
    set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.3.0")
    objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=Northwind;integrated security=SSPI"
    objBL.ErrorLogFile = "c:\error.log"
    objBL.CheckConstraints = True
    objBL.Transaction=True
    objBL.TempFilePath="\\server\folder"
    objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"
    set objBL=Nothing
    
  5. Execute the VBScript code.

    The schema must specify the corresponding sql:datatype for the CustomerID attribute when the value for CustomerID is specified as a GUID that includes braces ({ and }), such as:

    <ROOT>
    <Customers CustomerID="{6F9619FF-8B86-D011-B42D-00C04FC964FF}" 
               LastName="Smith" />
    </ROOT>
    

     This is the updated schema:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
      <xsd:element name="ROOT" sql:is-constant="true" >
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element ref="Customers" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    
      <xsd:element name="Customers" sql:relation="Cust" >
       <xsd:complexType>
         <xsd:attribute name="CustomerID"  type="xsd:string" 
                        sql:datatype="uniqueidentifier" />
         <xsd:attribute name="LastName" type="xsd:string" />
       </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    

    When sql:datatype is specified identifying the column type as uniqueidentifier, the bulk load operation removes the braces ({ and }) from the CustomerID value before inserting it in the column.

This is the equivalent XDR schema:

<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data" 
        xmlns:dt="urn:schemas-microsoft-com:datatypes"  
        xmlns:sql="urn:schemas-microsoft-com:xml-sql" >
<ElementType name="ROOT" sql:is-constant="1">
      <element type="Customers" />
</ElementType>
<ElementType name="Customers" sql:relation="Cust" >
  <AttributeType name="CustomerID"  sql:datatype="uniqueidentifier" />
  <AttributeType name="LastName"   />

  <attribute type="CustomerID" />
  <attribute type="LastName"   />
</ElementType>
</Schema>
I. Using an existing database connection with the ConnectionCommand property

You can use an existing ADO connection to bulk load XML. This is useful if XML Bulk Load is just one of many operations that will be performed on a data source.

The ConnectionCommand property enables you to use an existing ADO connection by using an ADO command object. This is illustrated in the following Visual Basic example:

Private Sub Form_Load()
Dim objBL As New SQLXMLBulkLoad3
Dim objCmd As New ADODB.Command
Dim objConn As New ADODB.Connection

'Open a connection to an instance of SQL Server.
objConn.Open "provider=SQLOLEDB;data source=(local);database=Database;uid=UserName;pwd=UserPassword"
'Ask the Command object to use the connection just established.
Set objCmd.ActiveConnection = objConn

'Tell Bulk Load to use the active command object that is using the Connection obj.
objBL.ConnectionCommand = objCmd
objBL.ErrorLogFile = "c:\error.log"
objBL.CheckConstraints = True
'The Transaction property must be set to True if you use ConnectionCommand.
objBL.Transaction = True
objBL.Execute "c:\SampleSchema.xml", "c:\SampleData.xml"
Set objBL = Nothing
End Sub

To test a working sample

  1. Create two tables:
    CREATE TABLE Cust(
                   CustomerID   varchar(5) PRIMARY KEY,
                   CompanyName  varchar(30),
                   City         varchar(20))
    GO
    CREATE TABLE CustOrder(
                   CustomerID  varchar(5) references Cust (CustomerID),
                   OrderID     varchar(5) PRIMARY KEY)
    GO
    
  2. Create a file in Notepad, and save it as SampleSchema.xml. Add the following XSD schema to the file:
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
    <xsd:annotation>
      <xsd:appinfo>
        <sql:relationship name="CustCustOrder"
              parent="Cust"
              parent-key="CustomerID"
              child="CustOrder"
              child-key="CustomerID" />
      </xsd:appinfo>
    </xsd:annotation>
      <xsd:element name="ROOT" sql:is-constant="true" >
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element ref="Customers" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="Customers" sql:relation="Cust" >
       <xsd:complexType>
         <xsd:sequence>
           <xsd:element name="CustomerID"  type="xsd:integer" />
           <xsd:element name="CompanyName" type="xsd:string" />
           <xsd:element name="City"        type="xsd:string" />
           <xsd:element name="Order" 
                              sql:relation="CustOrder"
                              sql:relationship="CustCustOrder" >
             <xsd:complexType>
              <xsd:attribute name="OrderID" type="xsd:integer" />
              <xsd:attribute name="CustomerID" type="xsd:integer" />
             </xsd:complexType>
           </xsd:element>
         </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    
  3. Create a file in Notepad, and save it as SampleXMLData.xml. Add the following XML document to the file:
    <ROOT>
      <Customers>
        <CustomerID>1111</CustomerID>
        <CompanyName>Hanari Carnes</CompanyName>
        <City>NY</City>
        <Order OrderID="1" />
        <Order OrderID="2" />
      </Customers>
    
      <Customers>
        <CustomerID>1112</CustomerID>
        <CompanyName>Toms Spezialitten</CompanyName>
         <City>LA</City>
        <Order OrderID="3" />
      </Customers>
      <Customers>
        <CustomerID>1113</CustomerID>
        <CompanyName>Victuailles en stock</CompanyName>
        <Order OrderID="4" />
    </Customers>
    </ROOT>
    
  4. Create a Visual Basic (Standard EXE) application and the preceding code. Add these references to the project:
    Microsoft XML BulkLoad for SQL Server 3.0 Type Library
    Microsoft ActiveX Data objects 2.6 Library
    
  5. Execute the application.
 

This is the equivalent XDR schema:

<?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="Cust"  >
      <element type="CustomerID" sql:field="CustomerID" />
      <element type="CompanyName" sql:field="CompanyName" />
      <element type="City" sql:field="City" />
      <element type="Order" >
         <sql:relationship
                key-relation="Cust"
                key="CustomerID"
                foreign-key="CustomerID"
                foreign-relation="CustOrder" />
      </element>
   </ElementType>
    <ElementType name="Order" sql:relation="CustOrder" >
      <AttributeType name="OrderID" />
      <AttributeType name="CustomerID" />
      <attribute type="OrderID" />
      <attribute type="CustomerID" />
    </ElementType>
</Schema>
Show:
© 2014 Microsoft