Partager via


sql:overflow-field (SQLXML 4.0)

In a schema, you can identify a column as an overflow column to receive all unconsumed data from the XML document. This column is specified in the schema by using the sql:overflow-field annotation. It is possible to have multiple overflow columns.

Whenever an XML node (element or attribute) for which there is a sql:overflow-field annotation defined enters into scope, the overflow column is activated and receives unconsumed data. When the node goes out of scope, the overflow column is no longer active and XML Bulk Load makes the previous overflow field (if any) active.

As it stores data in the overflow column, XML Bulk Load also stores the opening and closing tags of the parent element for which sql:overflow-field is defined.

For example, the following schema describes the <Customers> and <CustOrder> elements. Each of these elements identifies an overflow column:

<?xml version="1.0" ?>
<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="1">
  <xsd:complexType>
    <xsd:sequence> 
      <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"
                          sql:overflow-field="OverflowColumn">
               <xsd:complexType>
                 <xsd:attribute name="OrderID"/>
                 <xsd:attribute name="CustomerID"/>
               </xsd:complexType>
             </xsd:element>
          </xsd:sequence> 
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
 </xsd:element>
</xsd:schema>

In the schema, the <Customer> element maps to the Cust table and the <Order> element maps to the CustOrder table.

Both the <Customer> and <Order> elements identify an overflow column. Thus, XML Bulk Load saves all the unconsumed child elements and attributes of the <Customer> element in the overflow column of the Cust table, and all the unconsumed child elements and attributes of the <Order> element in the overflow column of the CustOrder table.

To test a working sample

  1. Save the schema that is provided in this example as SampleSchema.xml.

  2. Create these 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),
              OverflowColumn nvarchar(200))
    GO
    
  3. Save the following sample XML data as SampleXMLData.xml:

    <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>
       <City><![CDATA[LA]]> </City>
       <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. To execute XML Bulk Load, save and execute this Microsoft Visual Basic Scripting Edition (VBScript) example as Sample.vbs:

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