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

適用於:SQL ServerAzure SQL Database

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

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

注意

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

處理資料類型

如果架構使用sql:datatype) 指定imagebinaryVarbinary SQL Server資料類型 (,而且未指定 XML 資料類型,updategram 會假設 XML 資料類型為binary base 64。 如果您的資料是 bin.base 類型,您必須明確指定類型 (dt:type=bin.basetype=「xsd:hexBinary」) 。

如果架構指定dateTimedatetime 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 會使用與架構中所定義的相同元素名稱< 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 架構,在 order 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 中指定的父子關聯性來處理更新,以及如何使用 反向 注釋。 如需 反向 批註的詳細資訊,請參閱 在 sql:relationship 上指定 sql:inverse 屬性 (SQLXML 4.0)

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

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

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

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 邏輯在更新作業期間反轉父子式關聯性,則會在< 關聯 >性專案上指定反向批註。 因此,記錄會先加入到 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 查詢

另請參閱

Updategram 安全性考量 (SQLXML 4.0)