在 Updategram 中指定批注對應架構 (SQLXML 4.0)

適用於:SQL ServerAzure SQL 資料庫

本主題說明如何使用 Updategram 中指定的對應架構 (XSD 或 XDR) 來處理更新。 在 updategram 中,您可以提供批注對應架構的名稱,以用於將 updategram 中的元素和屬性對應至 Microsoft sql Server 中的數據表和數據行。 在 updategram 中指定對應架構時,updategram 中指定的專案和屬性名稱必須對應至對應架構中的元素和屬性。

若要指定對應架構,您可以使用 sync<>mapping-schema 屬性。 下列範例顯示兩個 updategram:一個使用簡單對應架構,另一個使用更複雜的架構。

注意

本檔假設您已熟悉 SQL Server 中的範本和對應架構支援。 如需詳細資訊,請參閱 批注式 XSD 架構簡介 (SQLXML 4.0) 。 如需使用 XDR 的舊版應用程式,請參閱批注式 XDR 架構(在 SQLXML 4.0 中已被取代)。

處理數據類型

如果架構指定映像、二進位或 varbinarySQL Server 數據類型(使用 sql:datatype),而且未指定 XML 數據類型,Updategram 會假設 XML 數據類型是二進位基底 64。 如果您的數據是 bin.base 類型,您必須明確指定類型 (dt:type=bin.base 或 type=“xsd:hexBinary”)。

如果架構指定 dateTime、 date 或 time XSD 數據類型,您也必須使用 sql:datatype=“dateTime” 來指定對應的 SQL Server 數據類型。

處理 SQL Server money 類型的參數時,您必須在對應架構中適當節點上明確指定 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 會使用與架構中所定義的相同元素名稱 <。> 這是必要的,因為 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) 包含兩個元素:<> 和<>

<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 的新訂單詳細數據記錄(<> 元素)。 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 中指定的父子關聯性來處理更新,以及 如何使用反向 批注。 如需反向批注的詳細資訊,請參閱在 sql:relationship 上指定 sql:inverse 屬性(SQLXML 4.0)。

此範例使用了目標樣本或測試資料庫中的表格:

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

  • Ord (OrderID, CustomerID),其中 CustomerID 是參考數據表中主鍵的CustomerIDCust外鍵。

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 架構具有 <> 和> 元素,並指定兩個元素之間的父子關聯性。 它會將 <> 識別為父元素,並將< 識別為子元素。>

Updategram 處理邏輯會使用父子關聯性的相關信息來判斷記錄插入數據表的順序。 在此範例中,updategram 邏輯會先嘗試將記錄插入 Ord 數據表(因為 <> 是父數據表),然後嘗試將記錄插入 Cust 數據表(因為 是子數據表)。< 不過,由於資料庫數據表架構中包含的主鍵/外鍵資訊,此插入作業會導致資料庫中發生外鍵違規,而且插入失敗。

若要指示 updategram 邏輯在更新作業期間反轉父子式關聯性,則會<批注。 因此,系統會先在 Cust 數據表中新增記錄,然後在 Ord 數據表中新增記錄,作業會成功。

下列 updategram 會使用指定的 XSD 架構,在 Ord 數據表中插入訂單 (OrderID=2),並在 Cust 數據表中插入客戶 (CustomerID='AAAAA') :

<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. 在所需的樣本或測試資料庫中建立以下表格:

    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 查詢。