共用方式為


使用 XML Updategram 更新資料 (SQLXML 4.0)

適用於:SQL ServerAzure SQL 資料庫

當您更新現有的數據時,您必須同時指定<前後><>區塊。 區塊前後<>>指定的<專案會描述所需的變更。 updategram 會使用在 前>區塊中指定的 <元素,來識別資料庫中的現有記錄。 after> 區塊中的<對應專案會指出記錄在執行更新作業后應該如何尋找。 從這項資訊中,updategram 會建立符合後>置區塊的 <SQL 語句。 Updategram 接著會使用此語句來更新資料庫。

這是更新作業的 updategram 格式:

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
<updg:sync [mapping-schema="SampleSchema.xml"]  >  
   <updg:before>  
      <ElementName [updg:id="value"] .../>  
      [<ElementName [updg:id="value"] .../> ... ]  
   </updg:before>  
   <updg:after>  
      <ElementName [updg:id="value"] ... />  
      [<ElementName [updg:id="value"] .../> ...]  
   </updg:after>  
</updg:sync>  
</ROOT>  

<updg:before>
前>區塊中的<元素會識別資料庫數據表中的現有記錄。

<updg:after>
after> 區塊中的<元素描述之前區塊中指定的><記錄應該如何套用更新。

mapping-schema 屬性會識別 updategram 要使用的對應架構。 如果 updategram 指定對應架構,在 區塊前後<>>指定的<元素和屬性名稱必須符合架構中的名稱。 對應架構會將這些元素或屬性名稱對應至資料庫數據表和數據行名稱。

如果 updategram 未指定架構,updategam 會使用預設對應。 在預設對應中, <updategram 中指定的 ElementName> 會對應至資料庫數據表,而子專案或屬性會對應至資料庫數據行。

前>區塊中的<元素必須只與資料庫中的一個數據表數據列相符。 如果元素符合多個數據表數據列或不符合任何數據表數據列,updategram 會傳回錯誤並取消整個 <同步> 區塊。

Updategram 可以包含多個 <同步> 區塊。 每個 <同步> 區塊都會被視為交易。 每個 <同步> 區塊可以有多個 <之前><之後> 的區塊。 例如,如果您要更新其中兩個現有記錄,則可以在配對前後<>>指定兩<個,每個要更新的記錄各指定一筆。

使用 updg:id 屬性

在前後>><區塊中<指定多個元素時,請使用 updg:id 屬性來標記前後><>區塊中的數據<列。 處理邏輯會使用這項資訊來判斷前>區塊配對中的<記錄與後>區塊中的<記錄。

如果下列任一項存在,則不需要 updg:id 屬性(雖然建議使用):

  • 指定之對應架構中的元素具有 定義於其上的 sql:key-fields 屬性。

  • Updategram 中提供一或多個特定值給索引鍵字段。

如果是這種情況,Updategram 會使用 sql:key-fields 中指定的索引鍵數據行,在區塊前後><>配對元素。<

如果對應架構無法識別索引鍵數據行(使用 sql:key-fields)或 Updategram 正在更新索引鍵數據行值,您必須指定 updg:id

在區塊前後<>>識別的<記錄不必以相同順序。 updg:id 屬性會強制在區塊前後><>指定的<項目之間建立關聯。

如果您在 before> 區塊中<指定一個元素,而後區塊中><只有一個對應的元素,則不需要使用 updg:id。 不過,建議您無論如何都指定 updg:id 以避免模棱兩可。

範例

使用 updategram 範例之前,請注意下列事項:

  • 大部分的範例都使用預設對應(也就是說,updategram 中未指定任何對應架構)。 如需使用對應架構之 Updategram 的更多範例,請參閱 在 Updategram 中指定批注對應架構 (SQLXML 4.0)

  • 大部分的範例都使用 AdventureWorks 範例資料庫。 所有更新都會套用至此資料庫中的數據表。 您可以還原 AdventureWorks 資料庫。

A. 更新記錄

下列 Updategram 會將 AdventureWorks 資料庫中 Person.Contact 數據表中的員工姓氏更新為 Fuller。 updategram 未指定任何對應架構;因此,Updategram 會使用預設對應。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
<updg:sync >  
<updg:before>  
   <Person.Contact ContactID="1" />  
</updg:before>  
<updg:after>  
   <Person.Contact LastName="Abel-Achong" />  
</updg:after>  
</updg:sync>  
</ROOT>  

前>區塊中所述<的記錄代表資料庫中目前的記錄。 updategram 會使用 之前> 區塊中指定的<所有數據行值來搜尋記錄。 在此 updategram 中 <,before> 區塊只提供 ContactID 數據行;因此,updategram 只會使用 值來搜尋記錄。 如果您要將 LastName 值新增至此區塊,updategram 會同時使用 ContactID 和 LastName 值來搜尋。

在此 updategram 中 <,after> 區塊只提供 LastName 數據行值,因為這是唯一正在變更的值。

測試 updategram
  1. 複製上述 Updategram 範本,並將其貼到文字檔中。 將檔案儲存為UpdateLastName.xml。

  2. 建立並使用 SQLXML 4.0 測試腳本 (Sqlxml4test.vbs) 來執行 updategram。

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

B. 使用 updg:id 屬性更新多個記錄

在此範例中,updategram 會在 AdventureWorks 資料庫中的 HumanResources.Shift 數據表上執行兩個更新:

  • 它會將上午 7:00 開始的原始日期班次名稱從 “Day” 變更為 “Early Morning”。

  • 它會插入名為 「Late Morning」 的新班次,從上午 10:00 開始。

在 updategram 中,updg:id 屬性會在前後><>區塊中的<項目之間建立關聯。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync >  
    <updg:before>  
       <HumanResources.Shift updg:id="x" Name="Day" />  
    </updg:before>  
    <updg:after>  
      <HumanResources.Shift updg:id="y" Name="Late Morning"   
                            StartTime="1900-01-01 10:00:00.000"  
                            EndTime="1900-01-01 18:00:00.000"  
                            ModifiedDate="2004-06-01 00:00:00.000"/>  
      <HumanResources.Shift updg:id="x" Name="Early Morning" />  
    </updg:after>  
  </updg:sync>  
</ROOT>  

請注意 updg:id 屬性如何將 before> 區塊中< HumanResources.Shift 元素的第一個實例<與後>區塊中< HumanResources.Shift>> 元素的第二個實例<配對。

測試 updategram
  1. 複製上述 Updategram 範本,並將其貼到文字檔中。 將檔案儲存為UpdateMultipleRecords.xml。

  2. 建立並使用 SQLXML 4.0 測試腳本 (Sqlxml4test.vbs) 來執行 updategram。

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

C. 指定區塊前後><的>多個<

若要避免模棱兩可,您可以在範例 B 中使用多個<>之前和<之後>的區塊配對來撰寫 updategram。 在 <配對之前><之後> 指定配對是指定多個更新的其中一種方式,最少混淆。 此外,如果每個前後<>>區塊<最多指定一個元素,您就不需要使用 updg:id 屬性。

注意

若要形成配對,<之後>標記必須緊接在標記之前的對應<之後。>

在下列 updategram 中,配對前>和<之後>的第一個<更新日班的班次名稱。 第二組會插入新的換班記錄。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync >  
    <updg:before>  
       <HumanResources.Shift ShiftID="1" Name="Day" />  
    </updg:before>  
    <updg:after>  
      <HumanResources.Shift Name="Early Morning" />  
    </updg:after>  
    <updg:before>  
    </updg:before>  
    <updg:after>  
      <HumanResources.Shift Name="Late Morning"   
                            StartTime="1900-01-01 10:00:00.000"  
                            EndTime="1900-01-01 18:00:00.000"  
                            ModifiedDate="2004-06-01 00:00:00.000"/>  
    </updg:after>  
  </updg:sync>  
</ROOT>  
測試 updategram
  1. 複製上述 Updategram 範本,並將其貼到文字檔中。 將檔案儲存為UpdateMultipleBeforeAfter.xml。

  2. 建立並使用 SQLXML 4.0 測試腳本 (Sqlxml4test.vbs) 來執行 updategram。

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

D. 指定多個 <同步> 區塊

您可以在 Updategram 中指定多個 <同步> 區塊。 每個 <指定的同步> 區塊都是獨立的交易。

在下列 Updategram 中,第一個 <同步> 區塊會更新 Sales.Customer 數據表中的記錄。 為了簡單起見,updategram 只指定必要的數據行值;識別值 (CustomerID) 和要更新的值 (SalesPersonID)。

第二個 <同步> 區塊會將兩筆記錄新增至 Sales.SalesOrderHeader 數據表。 在此數據表中,SalesOrderID 是 IDENTITY 類型數據行。 因此,updategram 不會在每個 Sales.SalesOrderHeader> 元素中<指定 SalesOrderID 的值。

指定多個同步區塊很有用,因為如果第二<個同步>>區塊(交易)無法將記錄新增至 Sales.SalesOrderHeader 數據表,則第一個<同步>區塊仍然可以更新 Sales.Customer 數據表中的客戶記錄。<

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync >  
    <updg:before>  
      <Sales.Customer CustomerID="1" SalesPersonID="280" />  
    </updg:before>  
    <updg:after>  
      <Sales.Customer CustomerID="1" SalesPersonID="283" />  
    </updg:after>  
  </updg:sync>  
  <updg:sync >  
    <updg:before>  
    </updg:before>  
    <updg:after>  
   <Sales.SalesOrderHeader   
             CustomerID="1"  
             RevisionNumber="1"  
             OrderDate="2004-07-01 00:00:00.000"  
             DueDate="2004-07-13 00:00:00.000"  
             OnlineOrderFlag="0"  
             ContactID="378"  
             BillToAddressID="985"  
             ShipToAddressID="985"  
             ShipMethodID="5"  
             SubTotal="24643.9362"  
             TaxAmt="1971.5149"  
             Freight="616.0984"  
             rowguid="01010101-2222-3333-4444-556677889900"  
             ModifiedDate="2004-07-08 00:00:00.000" />  
   <Sales.SalesOrderHeader  
             CustomerID="1"  
             RevisionNumber="1"  
             OrderDate="2004-07-01 00:00:00.000"  
             DueDate="2004-07-13 00:00:00.000"  
             OnlineOrderFlag="0"  
             ContactID="378"  
             BillToAddressID="985"  
             ShipToAddressID="985"  
             ShipMethodID="5"  
             SubTotal="1000.0000"  
             TaxAmt="0.0000"  
             Freight="0.0000"  
             rowguid="10101010-2222-3333-4444-556677889900"  
             ModifiedDate="2004-07-09 00:00:00.000" />  
    </updg:after>  
  </updg:sync>  
</ROOT>  
測試 updategram
  1. 複製上述 Updategram 範本,並將其貼到文字檔中。 將檔案儲存為UpdateMultipleSyncs.xml。

  2. 建立並使用 SQLXML 4.0 測試腳本 (Sqlxml4test.vbs) 來執行 updategram。

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

E. 使用對應架構

在此範例中,updategram 會使用 mapping-schema 屬性來指定對應架構 。 (沒有預設對應;也就是說,對應架構會提供Updategram中元素和屬性的必要對應至資料庫數據表和數據行。

updategram 中指定的元素和屬性是指對應架構中的元素和屬性。

下列 XSD 對應架構具有<對應><至資料庫中 Sales.Customer、Sales.SalesOrderHeader 和 Sales.SalesOrderDetail 數據表的客戶、訂單<>和 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="CustomerOrder"  
          parent="Sales.Customer"  
          parent-key="CustomerID"  
          child="Sales.SalesOrderHeader"  
          child-key="CustomerID" />  
  
    <sql:relationship name="OrderOD"  
          parent="Sales.SalesOrderHeader"  
          parent-key="SalesOrderID"  
          child="Sales.SalesOrderDetail"  
          child-key="SalesOrderID" />  
  </xsd:appinfo>  
</xsd:annotation>  
  
  <xsd:element name="Customer" sql:relation="Sales.Customer" >  
   <xsd:complexType>  
     <xsd:sequence>  
        <xsd:element name="Order"   
                     sql:relation="Sales.SalesOrderHeader"  
                     sql:relationship="CustomerOrder" >  
           <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:sequence>  
      <xsd:attribute name="CustomerID"   type="xsd:string" />   
    </xsd:complexType>  
  </xsd:element>  
</xsd:schema>  

此對應架構 (UpdategramMappingSchema.xml) 是在下列 updategram 中指定。 Updategram 會在 Sales.SalesOrderDetail 數據表中加入特定訂單的訂單詳細數據專案。 updategram 包含巢狀元素:<巢狀在 Order> 元素內<之 OD> 元素。 這兩個元素之間的主鍵/外鍵關聯性是在對應架構中指定。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync mapping-schema="UpdategramMappingSchema.xml" >  
    <updg:before>  
       <Order SalesOrderID="43659" />  
    </updg:before>  
    <updg:after>  
      <Order SalesOrderID="43659" >  
          <OD ProductID="776" UnitPrice="2329.0000"  
              OrderQty="2" UnitPriceDiscount="0.0" />  
      </Order>  
    </updg:after>  
  </updg:sync>  
</ROOT>  
測試 updategram
  1. 複製上述的對應架構,並將它貼到文本檔中。 將檔案儲存為UpdategramMappingSchema.xml。

  2. 複製上述 Updategram 範本,並將其貼到文字檔中。 將檔案儲存為與用來儲存對應架構的相同資料夾中的UpdateWithMappingSchema.xml(UpdategramMappingSchema.xml)。

  3. 建立並使用 SQLXML 4.0 測試腳本 (Sqlxml4test.vbs) 來執行 updategram。

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

如需使用對應架構之 Updategram 的更多範例,請參閱 在 Updategram 中指定批注對應架構 (SQLXML 4.0)

F. 搭配IDREFS屬性使用對應架構

此範例說明 Updategram 如何使用對應架構中的 IDREFS 屬性來更新多個數據表中的記錄。 在此範例中,假設資料庫是由下列數據表所組成:

  • Student(StudentID,LastName)

  • Course(CourseID,CourseName)

  • Enrollment(StudentID,CourseID)

因為學生可以註冊許多課程,而課程可以有許多學生,因此需要第三個表格註冊數據表來代表這個 M:N 關聯性。

下列 XSD 對應架構會使用 Student><Course> 和< Enrollment> 元素,提供數據表的 XML 檢視。< 對應架構中的IDREFS屬性會指定這些專案之間的關聯性。 Course> 元素上的< StudentIDList 屬性是 IDREFS 類型屬性,該屬性會參考 Enrollment 數據表中的 StudentID 數據行。 同樣地,Student> 元素上的< EnrollmentedIn 屬性是一個 IDREFS 類型屬性,參考 Enrollment 數據表中的 CourseID 數據行。

<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="StudentEnrollment"  
          parent="Student"  
          parent-key="StudentID"  
          child="Enrollment"  
          child-key="StudentID" />  
  
    <sql:relationship name="CourseEnrollment"  
          parent="Course"  
          parent-key="CourseID"  
          child="Enrollment"  
          child-key="CourseID" />  
  </xsd:appinfo>  
</xsd:annotation>  
  
  <xsd:element name="Course" sql:relation="Course"   
                             sql:key-fields="CourseID" >  
    <xsd:complexType>  
    <xsd:attribute name="CourseID"  type="xsd:string" />   
    <xsd:attribute name="CourseName"   type="xsd:string" />   
    <xsd:attribute name="StudentIDList" sql:relation="Enrollment"  
                 sql:field="StudentID"  
                 sql:relationship="CourseEnrollment"   
                                     type="xsd:IDREFS" />  
  
    </xsd:complexType>  
  </xsd:element>  
  <xsd:element name="Student" sql:relation="Student" >  
    <xsd:complexType>  
    <xsd:attribute name="StudentID"  type="xsd:string" />   
    <xsd:attribute name="LastName"   type="xsd:string" />   
    <xsd:attribute name="EnrolledIn" sql:relation="Enrollment"  
                 sql:field="CourseID"  
                 sql:relationship="StudentEnrollment"   
                                     type="xsd:IDREFS" />  
    </xsd:complexType>  
  </xsd:element>  
</xsd:schema>  

每當您在 Updategram 中指定此架構,並在 Course 數據表中插入記錄時,Updategram 會在 Course 數據表中插入新的課程記錄。 如果您為 StudentIDList 屬性指定一或多個新學生標識碼,updategram 也會在每個新學生的註冊數據表中插入一筆記錄。 updategram 可確保不會將重複專案新增至 Enrollment 數據表。

測試 updategram
  1. 在虛擬根目錄中指定的資料庫中建立這些資料表:

    CREATE TABLE Student(StudentID varchar(10) primary key,   
                         LastName varchar(25))  
    CREATE TABLE Course(CourseID varchar(10) primary key,   
                        CourseName varchar(25))  
    CREATE TABLE Enrollment(StudentID varchar(10)   
                                      references Student(StudentID),  
                           CourseID varchar(10)   
                                      references Course(CourseID))  
    
  2. 新增此範例資料:

    INSERT INTO Student VALUES ('S1','Davoli')  
    INSERT INTO Student VALUES ('S2','Fuller')  
    
    INSERT INTO Course VALUES  ('CS101', 'C Programming')  
    INSERT INTO Course VALUES  ('CS102', 'Understanding XML')  
    
    INSERT INTO Enrollment VALUES ('S1', 'CS101')  
    INSERT INTO Enrollment VALUES ('S1', 'CS102')  
    
  3. 複製上述的對應架構,並將它貼到文本檔中。 將檔案儲存為SampleSchema.xml。

  4. 將 updategram (SampleUpdategram) 儲存在上一個步驟中用來儲存對應架構的相同資料夾中。 (這個 Updategram 會從 CS102 課程中卸除 StudentID=“1” 的學生。

    <ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
      <updg:sync mapping-schema="SampleSchema.xml" >  
        <updg:before>  
            <Student updg:id="x" StudentID="S1" LastName="Davolio"  
                                 EnrolledIn="CS101 CS102" />  
        </updg:before>  
        <updg:after >  
            <Student updg:id="x" StudentID="S1" LastName="Davolio"  
                                 EnrolledIn="CS101" />  
        </updg:after>  
      </updg:sync>  
    </ROOT>  
    
  5. 建立並使用 SQLXML 4.0 測試腳本 (Sqlxml4test.vbs) 來執行 updategram。

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

  6. 儲存並執行下列 Updategram,如先前步驟所述。 Updategram 會將 StudentID=“1” 的學生新增至 CS102 課程,方法是在註冊數據表中新增記錄。

    <ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
      <updg:sync mapping-schema="SampleSchema.xml" >  
        <updg:before>  
            <Student updg:id="x" StudentID="S1" LastName="Davolio"  
                                 EnrolledIn="CS101" />  
        </updg:before>  
        <updg:after >  
            <Student updg:id="x" StudentID="S1" LastName="Davolio"  
                                 EnrolledIn="CS101 CS102" />  
        </updg:after>  
      </updg:sync>  
    </ROOT>  
    
  7. 儲存並執行下一個 Updategram,如先前步驟所述。 此 Updategram 會插入三名新學生,並在 CS101 課程中註冊。 同樣地,IDREFS 關聯性會在註冊數據表中插入記錄。

    <ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
      <updg:sync mapping-schema="SampleSchema.xml" >  
        <updg:before>  
           <Course updg:id="y" CourseID="CS101"   
                               CourseName="C Programming" />  
        </updg:before>  
        <updg:after >  
           <Student updg:id="x1" StudentID="S3" LastName="Leverling" />  
           <Student updg:id="x2" StudentID="S4" LastName="Pecock" />  
           <Student updg:id="x3" StudentID="S5" LastName="Buchanan" />  
           <Course updg:id="y" CourseID="CS101"  
                               CourseName="C Programming"  
                               StudentIDList="S3 S4 S5" />  
        </updg:after>  
      </updg:sync>  
    </ROOT>  
    

這是對等的 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="Enrollment" sql:relation="Enrollment" sql:key-fields="StudentID CourseID">  
    <AttributeType name="StudentID" dt:type="id" />  
    <AttributeType name="CourseID" dt:type="id" />  
  
    <attribute type="StudentID" />  
    <attribute type="CourseID" />  
  </ElementType>  
  <ElementType name="Course" sql:relation="Course" sql:key-fields="CourseID">  
    <AttributeType name="CourseID" dt:type="id" />  
    <AttributeType name="CourseName" />  
  
    <attribute type="CourseID" />  
    <attribute type="CourseName" />  
  
    <AttributeType name="StudentIDList" dt:type="idrefs" />  
    <attribute type="StudentIDList" sql:relation="Enrollment" sql:field="StudentID" >  
        <sql:relationship  
                key-relation="Course"  
                key="CourseID"  
                foreign-relation="Enrollment"  
                foreign-key="CourseID" />  
    </attribute>  
  
  </ElementType>  
  <ElementType name="Student" sql:relation="Student">  
    <AttributeType name="StudentID" dt:type="id" />  
     <AttributeType name="LastName" />  
  
    <attribute type="StudentID" />  
    <attribute type="LastName" />  
  
    <AttributeType name="EnrolledIn" dt:type="idrefs" />  
    <attribute type="EnrolledIn" sql:relation="Enrollment" sql:field="CourseID" >  
        <sql:relationship  
                key-relation="Student"  
                key="StudentID"  
                foreign-relation="Enrollment"  
                foreign-key="StudentID" />  
    </attribute>  
  
    <element type="Enrollment" sql:relation="Enrollment" >  
        <sql:relationship key-relation="Student"  
                          key="StudentID"  
                          foreign-relation="Enrollment"  
                          foreign-key="StudentID" />  
    </element>  
  </ElementType>  
  
</Schema>  

如需使用對應架構之 Updategram 的更多範例,請參閱 在 Updategram 中指定批注對應架構 (SQLXML 4.0)

另請參閱

Updategram 安全性考慮 (SQLXML 4.0)