共用方式為


在 Updategram 中指定註解式對應結構描述 (SQLXML 4.0)

本主題說明 Updategram 中指定的對應結構描述 (XSD 或 XDR) 要如何用來處理更新。在 updategram 中,您可以提供註解式對應結構描述的名稱,用來將 updategram 中的元素和屬性對應到 Microsoft SQL Server 中的資料表和資料行。在 updategram 中指定對應結構描述時,此 updategram 中指定的元素和屬性名稱必須對應到對應結構描述內的元素和屬性。

若要指定對應結構描述,您可使用 <sync> 元素的 mapping-schema 屬性。下列範例會示範兩個 updategram:使用簡單對應結構描述的 updategram 以及使用更複雜之結構描述的 updategram。

[!附註]

本文件集假設您非常熟悉 SQL Server 中的範本和對應結構描述支援。如需詳細資訊,請參閱<註解式 XSD 結構描述簡介 (SQLXML 4.0)>。如需了解使用 XDR 的舊版應用程式,請參閱<註解式 XDR 結構描述 (在 SQLXML 4.0 中已被取代)>。

處理資料類型

如果此結構描述指定 image、binary 或 varbinarySQL Server 資料類型 (藉由使用 sql:datatype),而且未指定 XML 資料類型,此 updategram 會假設 XML 資料類型為 binary base 64。如果您的資料是 bin.base 類型,您必須明確指定此類型 (dt:type=bin.base 或 type="xsd:hexBinary")。

如果此結構描述指定 dateTime、date 或 time XSD 資料類型,您也必須使用 sql:datatype="dateTime" 來指定對應的 SQL Server 資料類型。

當處理 SQL Servermoney 類型的參數時,您必須在對應結構描述內的適當節點上明確指定 sql:datatype="money"。

範例

若要使用下列範例建立工作範例,您必須符合<執行 SQLXML 範例的需求>中指定的需求。

A. 使用簡單對應結構描述建立 updategram

下列 XSD 結構描述 (SampleSchema.xml) 是一種對應結構描述,可將 <Customer> 元素對應至 Sales.Customer 資料表:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
  <xsd:element name="Customer" sql:relation="Sales.Customer" >
   <xsd:complexType>
        <xsd:attribute name="CustID"  
                       sql:field="CustomerID" 
                       type="xsd:string" />
        <xsd:attribute name="RegionID"  
                       sql:field="TerritoryID"  
                       type="xsd:string" />
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

下列 updategram 會將一筆記錄插入 Sales.Customer 資料表,並依賴之前的對應結構描述,適當地將此資料對應至資料表。請注意,updategram 會使用相同的元素名稱 <Customer>,如同結構描述內所定義。這是強制性的作法,因為 updategram 會指定特定的結構描述。

測試 Updategram

  1. 複製上述的結構描述程式碼,並將其貼到文字檔中。將檔案儲存為 SampleUpdateSchema.xml。

  2. 複製底下的 updategram 範本,並將其貼到文字檔中。將檔案儲存為 SampleUpdategram.xml 並放在與 SampleUpdateSchema.xml 相同的目錄中。

    <ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">
      <updg:sync mapping-schema="SampleUpdateSchema.xml">
        <updg:before>
          <Customer CustID="1" RegionID="1"  />
        </updg:before>
        <updg:after>
          <Customer CustID="1" RegionID="2" />
        </updg:after>
      </updg:sync>
    </ROOT>
    

    針對對應結構描述 (SampleUpdateSchema.xml) 指定的目錄路徑相對於儲存範本的目錄。也可以指定絕對路徑,例如:

    mapping-schema="C:\SqlXmlTest\SampleUpdateSchema.xml"
    
  3. 建立及使用 SQLXML 4.0 測試指令碼 (Sqlxml4test.vbs) 來執行範本。

    如需詳細資訊,請參閱<使用 ADO 執行 SQLXML 4.0 查詢>。

這是相等的 XDR 結構描述:

<?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="Customer" sql:relation="Sales.Customer" >
       <AttributeType name="CustID" />
       <AttributeType name="RegionID" />

       <attribute type="CustID" sql:field="CustomerID" />
       <attribute type="RegionID" sql:field="TerritoryID" />
     </ElementType>
   </Schema> 

B. 使用對應結構描述內指定的父子式關聯性插入記錄

結構描述元素可以產生關聯。<sql:relationship> 元素會指定結構描述元素之間的父子式關聯性。這項資訊是用來更新具有主索引鍵與外部索引鍵關聯性的對應資料表。

下列對應結構描述 (SampleSchema.xml) 是由兩個元素所組成:<Order><OD>

<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="Sales.SalesOrderHeader"
          parent-key="SalesOrderID"
          child="Sales.SalesOrderDetail"
          child-key="SalesOrderID" />
  </xsd:appinfo>
</xsd:annotation>

  <xsd:element name="Order" sql:relation="Sales.SalesOrderHeader" >
   <xsd:complexType>
     <xsd:sequence>
        <xsd:element name="OD" 
                     sql:relation="Sales.SalesOrderDetail"
                     sql:relationship="OrderOD" >
           <xsd:complexType>
              <xsd:attribute name="SalesOrderID"   type="xsd:integer" />
              <xsd:attribute name="ProductID" type="xsd:integer" />
             <xsd:attribute name="UnitPrice"  type="xsd:decimal" />
             <xsd:attribute name="OrderQty"   type="xsd:integer" />
             <xsd:attribute name="UnitPriceDiscount"   type="xsd:decimal" />

           </xsd:complexType>
        </xsd:element>
     </xsd:sequence>
        <xsd:attribute name="CustomerID"   type="xsd:string" /> 
        <xsd:attribute name="SalesOrderID"  type="xsd:integer" />
        <xsd:attribute name="OrderDate"  type="xsd:date" />
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

下列 updategram 會使用這個 XSD 結構描述,針對訂單 43860 加入新的訂單詳細資料記錄 (<after> 區塊中的 <OD> 元素)。mapping-schema 屬性是用來指定 updategram 中的對應結構描述。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">
  <updg:sync mapping-schema="SampleUpdateSchema.xml" >
    <updg:before>
       <Order SalesOrderID="43860" />
    </updg:before>
    <updg:after>
      <Order SalesOrderID="43860" >
           <OD ProductID="753" UnitPrice="$10.00"
               Quantity="5" Discount="0.0" />
      </Order>
    </updg:after>
  </updg:sync>
</ROOT>

測試 Updategram

  1. 複製上述的結構描述程式碼,並將其貼到文字檔中。將檔案儲存為 SampleUpdateSchema.xml。

  2. 複製上述的 updategram 範本,並將其貼到文字檔中。將檔案儲存為 SampleUpdategram.xml 並放在與 SampleUpdateSchema.xml 相同的目錄中。

    針對對應結構描述 (SampleUpdateSchema.xml) 指定的目錄路徑相對於儲存範本的目錄。也可以指定絕對路徑,例如:

    mapping-schema="C:\SqlXmlTest\SampleUpdateSchema.xml"
    
  3. 建立及使用 SQLXML 4.0 測試指令碼 (Sqlxml4test.vbs) 來執行範本。

    如需詳細資訊,請參閱<使用 ADO 執行 SQLXML 4.0 查詢>。

這是相等的 XDR 結構描述:

<?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="OD" sql:relation="Sales.SalesOrderDetail" >
    <AttributeType name="SalesOrderID" />
    <AttributeType name="ProductID" />
    <AttributeType name="UnitPrice"  dt:type="fixed.14.4" />
    <AttributeType name="OrderQty" />
    <AttributeType name="UnitPriceDiscount" />

    <attribute type="SalesOrderID" />
    <attribute type="ProductID" />
    <attribute type="UnitPrice" />
    <attribute type="OrderQty" />
    <attribute type="UnitPriceDiscount" />
</ElementType>

<ElementType name="Order" sql:relation="Sales.SalesOrderHeader" >
    <AttributeType name="CustomerID" />
    <AttributeType name="SalesOrderID" />
    <AttributeType name="OrderDate" />

    <attribute type="CustomerID" />
    <attribute type="SalesOrderID" />
    <attribute type="OrderDate" />
    <element type="OD" >
             <sql:relationship 
                   key-relation="Sales.SalesOrderHeader"
                   key="SalesOrderID"
                   foreign-key="SalesOrderID"
                   foreign-relation="Sales.SalesOrderDetail" />
    </element>
</ElementType>
</Schema>

C. 使用 XSD 結構描述內指定的父子式關聯性和反向註解來插入記錄

這個範例會說明 updategram 邏輯要如何使用 XSD 中指定的父子式關聯性來處理更新,以及如何使用 inverse 註解。如需有關 inverse 註解的詳細資訊,請參閱<針對 sql:relationship 指定 sql:inverse 屬性 (SQLXML 4.0)>。

此範例假設下列資料表位於 tempdb 資料庫中:

  • Cust (CustomerID, CompanyName),其中 CustomerID 是主索引鍵

  • Ord (OrderID, CustomerID),其中 CustomerID 是參考 Cust 資料表內之 CustomerID 主索引鍵的外部索引鍵。

updategram 會使用以下 XSD 結構描述,將記錄插入 Cust 和 Ord 資料表中:

<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="OrdCust" inverse="true"
                  parent="Ord"
                  parent-key="CustomerID"
                  child-key="CustomerID"
                  child="Cust"/>
  </xsd:appinfo>
</xsd:annotation>

<xsd:element name="Order" sql:relation="Ord">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element ref="Customer" sql:relationship="OrdCust"/>
    </xsd:sequence>
    <xsd:attribute name="OrderID"   type="xsd:int"/>
    <xsd:attribute name="CustomerID" type="xsd:string"/>
  </xsd:complexType>
</xsd:element>

<xsd:element name="Customer" sql:relation="Cust">
  <xsd:complexType>
     <xsd:attribute name="CustomerID"  type="xsd:string"/>
    <xsd:attribute name="CompanyName" type="xsd:string"/>
  </xsd:complexType>
</xsd:element>

</xsd:schema>

此範例中的 XSD 結構描述具有 <Customer><Order> 元素,而且它會指定兩個元素之間的父子式關聯性。它會將 <Order> 識別為父元素,並將 <Customer> 識別為子元素。

此 updategram 處理邏輯會使用有關父子式關聯性的資訊來判斷哪些記錄會插入資料表中。在此範例中,updategram 邏輯會先嘗試將記錄插入 Ord 資料表 (因為 <Order> 是父元素),然後嘗試將記錄插入 Cust 資料表 (因為 <Customer> 是子元素)。但是,由於包含在資料庫資料表結構描述內之主索引鍵/外部索引鍵資訊的緣故,這項插入作業會造成資料庫中的外部索引鍵違規,而使得插入失敗。

為了指引 updategram 邏輯在更新作業期間反轉父子式關聯性,將會在 <relationship> 元素上指定 inverse 註解。因此,記錄會先加入到 Cust 資料表,然後再加入到 Ord 資料表,作業就會成功。

下列 updategram 會使用指定的 XSD 結構描述,將訂單 (OrderID=2) 插入到 Ord 資料表,並將客戶 (CustomerID='AAAAA') 插入到 Cust 資料表:

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">
  <updg:sync mapping-schema="SampleUpdateSchema.xml" >
    <updg:before/>
    <updg:after>
      <Order OrderID="2" CustomerID="AAAAA" >
        <Customer CustomerID="AAAAA" CompanyName="AAAAA Company" />
      </Order>
    </updg:after>
  </updg:sync>
</ROOT>

測試 Updategram

  1. tempdb 資料庫中建立這些資料表:

    USE tempdb
    CREATE TABLE Cust(CustomerID varchar(5) primary key, 
                      CompanyName varchar(20))
    GO
    CREATE TABLE Ord (OrderID int primary key, 
                      CustomerID varchar(5) references Cust(CustomerID))
    GO
    
  2. 複製上述的結構描述程式碼,並將其貼到文字檔中。將檔案儲存為 SampleUpdateSchema.xml。

  3. 複製上述的 updategram 範本,並將其貼到文字檔中。將檔案儲存為 SampleUpdategram.xml 並放在與 SampleUpdateSchema.xml 相同的目錄中。

    針對對應結構描述 (SampleUpdateSchema.xml) 指定的目錄路徑相對於儲存範本的目錄。也可以指定絕對路徑,例如:

    mapping-schema="C:\SqlXmlTest\SampleUpdateSchema.xml"
    
  4. 建立及使用 SQLXML 4.0 測試指令碼 (Sqlxml4test.vbs) 來執行範本。

    如需詳細資訊,請參閱<使用 ADO 執行 SQLXML 4.0 查詢>。