共用方式為


使用 XML Updategram 插入資料 (SQLXML 4.0)

適用於:SQL ServerAzure SQL 資料庫

Updategram 表示當記錄實例出現在 後區塊中<,但不出現在對應的<前>區塊時,插入作業。> 在此情況下,updategram 會將記錄插入 <後> 區塊中的 資料庫。

這是插入作業的 updategram 格式:

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

<Block 之前>

<插入作業可以省略 before> 區塊。 如果未指定選擇性 的對應架構 屬性, <updategram 中指定的 ElementName> 會對應至資料庫數據表,而子專案或屬性會對應至數據表中的數據行。

<Block 之後>

您可以在 後>區塊中<指定一或多個記錄。

<如果 after> 區塊未提供特定資料行的值,updategram 會使用批注架構中指定的預設值(如果已指定架構)。 如果架構未指定數據行的預設值,updategram 不會指定此數據行的任何明確值,而是將 Microsoft sql Server 預設值 (如果指定的話) 指派給此數據行。 如果沒有 SQL Server 預設值,且數據行接受 NULL 值,Updategram 會將數據行值設定為 NULL。 如果數據行沒有預設值,也沒有接受 NULL 值,則命令會失敗,而且 updategram 會傳回錯誤。 選擇性 updg:returnid 屬性是用來傳回系統在具有 IDENTITY 類型數據行的數據表中加入記錄時所產生的識別值。

updg:id 屬性

如果 updategram 只插入記錄,updategram 不需要 updg:id 屬性。 如需 updg:id 的詳細資訊,請參閱使用 XML Updategram 更新數據(SQLXML 4.0)。

updg:at-identity 屬性

當 Updategram 在具有 IDENTITY 類型數據行的數據表中插入記錄時,updategram 可以使用選擇性 updg:at-identity 屬性來擷取系統指派的值。 Updategram 接著可以在後續的作業中使用此值。 執行 updategram 時,您可以指定 updg:returnid 屬性來傳回所產生的識別值。

updg:guid 屬性

updg:guid 屬性是選擇性屬性,會產生全域唯一標識符。 此值會保留在指定之整個 <同步> 區塊的範圍內。 您可以在同步>區塊中的任何<位置使用此值。 屬性會呼叫 NEWGUID()SQL Server 函式來產生唯一標識碼。

範例

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

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

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

  • 大部分的範例都會 AdventureWorks2022 使用範例資料庫。 所有更新都會套用至此資料庫中的數據表。

A. 使用 Updategram 插入記錄

這個以屬性為中心的 Updategram 會在資料庫的 HumanResources.Employee 數據表中 AdventureWorks2022 插入記錄。

在此範例中,updategram 不會指定對應架構。 因此,updategram 會使用預設對應,其中元素名稱會對應至數據表名稱,而屬性或子項目會對應至該數據表中的數據行。

AdventureWorks2022 HumanResources.Department 數據表的架構會對所有數據行施加「非 Null」限制。 因此,updategram 必須包含針對所有數據行指定的值。 DepartmentID 是 IDENTITY 類型數據行。 因此,不會為其指定任何值。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync >  
    <updg:before>  
    </updg:before>  
    <updg:after>  
       <HumanResources.Department   
            Name="New Product Research"   
            GroupName="Research and Development"   
            ModifiedDate="2010-08-31"/>  
    </updg:after>  
  </updg:sync>  
</ROOT>  
若要針對架構測試範例 XPath 查詢
  1. 複製上述 Updategram,並將它貼到文字檔中。 將檔案儲存為MyUpdategram.xml。

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

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

在以元素為中心的對應中,updategram 看起來像這樣:

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync >  
    <updg:before>  
    </updg:before>  
    <updg:after>  
       <HumanResources.Department>  
            <Name> New Product Research </Name>  
            <GroupName> Research and Development </GroupName>  
            <ModifiedDate>2010-08-31</ModifiedDate>  
       </HumanResources.Department>  
    </updg:after>  
  </updg:sync>  
</ROOT>  

在混合模式 (元素中心和屬性中心) updategram 中,元素可以同時具有屬性和子元素,如此 updategram 所示:

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync >  
    <updg:before>  
    </updg:before>  
    <updg:after>  
       <HumanResources.Department   
            Name=" New Product Research "   
            <GroupName>Research and Development</GroupName>  
            <ModifiedDate>2010-08-31</ModifiedDate>  
       </HumanResources.Department>  
    </updg:after>  
  </updg:sync>  
</ROOT>  

B. 使用 Updategram 插入多個記錄

此 Updategram 會將兩個新的班次記錄新增至 HumanResources.Shift 數據表。 updategram 不會在 區塊之前>指定選擇性<的 。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync>  
    <updg:after >  
       <HumanResources.Shift Name="Day-Evening"  
                        StartTime="1900-01-01 11:00:00.000"  
                        EndTime="1900-01-01 19:00:00.000"  
                        ModifiedDate="2004-01-01 00:00:00.000" />  
       <HumanResources.Shift Name="Evening-Night"  
                        StartTime="1900-01-01 19:00:00.000"  
                        EndTime="1900-01-01 03:00:00.000"  
                        ModifiedDate="2004-01-01 00:00:00.000" />  
    </updg:after>  
  </updg:sync>  
</ROOT>  
若要針對架構測試範例 XPath 查詢
  1. 複製上述 Updategram,並將它貼到文字檔中。 將檔案儲存為Updategram-AddShifts.xml。

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

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

此範例的另一個版本是 updategram,它會使用兩個不同的 <後> 區塊,而不是一個區塊來插入這兩個員工。 這是有效的,而且可以編碼如下:

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync>  
    <updg:after >  
       <HumanResources.Shift Name="Day-Evening"  
                        StartTime="1900-01-01 11:00:00.000"  
                        EndTime="1900-01-01 19:00:00.000"  
                        ModifiedDate="2004-01-01 00:00:00.000" />  
    </updg:after>  
    <updg:before>  
    </updg:before>  
    <updg:after >  
       <HumanResources.Shift Name="Evening-Night"  
                        StartTime="1900-01-01 19:00:00.000"  
                        EndTime="1900-01-01 03:00:00.000"  
                        ModifiedDate="2004-01-01 00:00:00.000" />  
    </updg:after>  
  </updg:sync>  
</ROOT>  

C. 使用 XML 中無效的有效 SQL Server 字元

在 SQL Server 中,數據表名稱可以包含空格,例如 Northwind 資料庫中的 [訂單詳細數據] 資料表。 不過,這在有效的 SQL Server 識別碼但無效的 XML 識別碼中無效,可以使用 『__xHHHH__』 編碼值來編碼,其中 HHHH 代表字元的四位數十六進位 UCS-2 程式代碼,以最重要的位優先順序表示字元。

注意

此範例使用 Northwind 資料庫。 您可以使用可從此 Microsoft網站下載的 SQL 腳本來安裝 Northwind 資料庫。

此外,元素名稱必須以方括弧 ([ ]) 括住。 由於字元 [and] 在 XML 中無效,因此您必須將它們分別編碼為_x005B_和_x005D_。 (如果您使用對應架構,則可以提供不包含無效字元的項目名稱,例如空格符。對應架構會執行必要的對應;因此,您不需要為這些字元編碼。

此 Updategram 會將記錄新增至 Northwind 資料庫中的 [訂單詳細數據] 數據表:

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync >  
    <updg:before>  
    </updg:before>  
    <updg:after>  
      <_x005B_Order_x0020_Details_x005D_ OrderID="1"  
            ProductID="11"  
            UnitPrice="$1.0"  
            Quantity="1"  
            Discount="0.0" />  
    </updg:after>  
  </updg:sync>  
</ROOT>  

Order Details 數據表中的 UnitPrice 數據行是 money 類型。 若要套用適當的類型轉換(從字串類型到貨幣類型),必須將貨幣符號字元 ($) 新增為值的一部分。 如果 updategram 未指定對應架構,則會評估字串值的第一個字元。 如果第一個字元是貨幣符號 ($),則會套用適當的轉換。

如果根據對應架構指定 updategram,其中數據行會適當地標示為 dt:type=“fixed.14.4”sql:datatype=“money”,則不需要貨幣符號 ($), 而且對應會處理轉換。 這是確保發生適當類型轉換的建議方式。

若要針對架構測試範例 XPath 查詢
  1. 複製上述 Updategram,並將它貼到文字檔中。 將檔案儲存為UpdategramSpacesInTableName.xml。

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

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

D. 使用 at-identity 屬性來擷取已在 IDENTITY 類型數據行中插入的值

下列 Updategram 會插入兩筆記錄:一個在 Sales.SalesOrderHeader 數據表中,另一個則插入 Sales.SalesOrderDetail 數據表。

首先,updategram 會將記錄新增至 Sales.SalesOrderHeader 數據表。 在此數據表中,SalesOrderID 資料行是 IDENTITY 類型數據行。 因此,當您將此記錄新增至數據表時,updategram 會使用 at-identity 屬性,將指派的 SalesOrderID 值擷取為 “x” (佔位符值)。 updategam 接著會將這個 at-identity 變數指定為 Sales.SalesOrderID 元素中的 <SalesOrderID> 屬性值。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
 <updg:sync >  
  <updg:before>  
  </updg:before>  
  <updg:after>  
   <Sales.SalesOrderHeader updg:at-identity="x"   
             RevisionNumber="1"  
             OrderDate="2001-07-01 00:00:00.000"  
             DueDate="2001-07-13 00:00:00.000"  
             OnlineOrderFlag="0"  
             CustomerID="676"  
             ContactID="378"  
             BillToAddressID="985"  
             ShipToAddressID="985"  
             ShipMethodID="5"  
             SubTotal="24643.9362"  
             TaxAmt="1971.5149"  
             Freight="616.0984"  
             rowguid="00001111-2222-3333-4444-556677889900"  
             ModifiedDate="2001-07-08 00:00:00.000" />  
      <Sales.SalesOrderDetail SalesOrderID="x"  
                LineNumber="1"  
                OrderQty="1"  
                ProductID="776"  
                SpecialOfferID="1"  
                UnitPrice="2429.9928"  
                UnitPriceDiscount="0.00"  
                rowguid="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"  
                ModifiedDate="2001-07-01 00:00:00.000" />  
    </updg:after>  
  </updg:sync>  
</ROOT>  

如果您想要傳回 updg:at-identity 屬性所產生的識別值,您可以使用 updg:returnid 屬性。 以下是會傳回此識別值的修訂 Updategram。 (這個 Updategram 會新增兩筆訂單記錄和兩筆訂單詳細數據記錄,只是為了讓範例更加複雜。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
 <updg:sync>  
  <updg:before>  
  </updg:before>  
  <updg:after updg:returnid="x y" >  
       <HumanResources.Shift updg:at-identity="x" Name="Day-Evening"  
                        StartTime="1900-01-01 11:00:00.000"  
                        EndTime="1900-01-01 19:00:00.000"  
                        ModifiedDate="2004-01-01 00:00:00.000" />  
       <HumanResources.Shift updg:at-identity="y" Name="Evening-Night"  
                        StartTime="1900-01-01 19:00:00.000"  
                        EndTime="1900-01-01 03:00:00.000"  
                        ModifiedDate="2004-01-01 00:00:00.000" />  
  </updg:after>  
 </updg:sync>  
</ROOT>  

執行 updategram 時,它會傳回類似下列的結果,其中包含產生的識別值(用於數據表識別的 ShiftID 數據行產生的值):

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">   
  <returnid>   
    <x>4</x>   
    <y>5</y>   
  </returnid>   
</ROOT>  
若要針對架構測試範例 XPath 查詢
  1. 複製上述 Updategram,並將它貼到文字檔中。 將檔案儲存為Updategram-returnId.xml。

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

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

E. 使用 updg:guid 屬性來產生唯一值

在此範例中,updategram 會在 Cust 和 CustOrder 數據表中插入記錄。 此外,updategram 會使用 updg:guid 屬性來產生 CustomerID 屬性的唯一值。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync >  
    <updg:before>  
    </updg:before>  
    <updg:after updg:returnid="x" >  
      <Cust updg:guid="x" >  
         <CustID>x</CustID>  
         <LastName>Fuller</LastName>  
      </Cust>  
      <CustOrder>  
         <CustID>x</CustID>  
         <OrderID>1</OrderID>  
      </CustOrder>  
    </updg:after>  
  </updg:sync>  
</ROOT>  

updategram 會 指定 returnid 屬性。 因此,會傳回產生的 GUID:

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <returnid>  
    <x>7111BD1A-7F0B-4CEE-B411-260DADFEFA2A</x>   
  </returnid>  
</ROOT>  
測試 updategram
  1. 複製上述 Updategram,並將它貼到文字檔中。 將檔案儲存為Updategram-GenerateGuid.xml。

  2. 建立這些資料表:

    USE tempdb  
    CREATE TABLE Cust (CustID uniqueidentifier, LastName varchar(20))  
    CREATE TABLE CustOrder (CustID uniqueidentifier, OrderID int)  
    
  3. 建立並使用 SQLXML 4.0 測試腳本 (Sqlxml4test.vbs) 來執行範本。

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

F. 在 Updategram 中指定架構

此範例中的 updategram 會將記錄插入下表:

CustOrder(OrderID, EmployeeID, OrderType)  

此 Updategram 中指定 XSD 架構(也就是說,沒有 Updategram 元素和屬性的預設對應)。 架構提供元素和屬性與資料庫數據表和數據行的必要對應。

下列架構 (CustOrderSchema.xml) 描述<由 OrderID 和 EmployeeID 屬性組成的 CustOrder> 元素。 為了讓架構更有趣,預設值會指派給 EmployeeID 屬性。 Updategram 只會針對插入作業使用屬性的預設值,然後只有在 updategram 未指定該屬性時。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
  <xsd:element name="CustOrder" >  
   <xsd:complexType>  
        <xsd:attribute name="OrderID"     type="xsd:integer" />   
        <xsd:attribute name="EmployeeID"  type="xsd:integer" />  
        <xsd:attribute name="OrderType  " type="xsd:integer" default="1"/>  
    </xsd:complexType>  
  </xsd:element>  
</xsd:schema>  

這個 Updategram 會將記錄插入 CustOrder 數據表。 updategram 只會指定 OrderID 和 EmployeeID 屬性值。 它未指定 OrderType 屬性值。 因此,updategram 會使用上述架構中指定的 EmployeeID 屬性預設值。

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"  
             xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
<updg:sync mapping-schema='CustOrderSchema.xml'>  
<updg:after>  
       <CustOrder OrderID="98000" EmployeeID="1" />  
</updg:after>  
</updg:sync>  
</ROOT>  

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

測試 updategram
  1. 在 tempdb 資料庫中建立此資料表:

    USE tempdb  
    CREATE TABLE CustOrder(  
                     OrderID int,   
                     EmployeeID int,   
                     OrderType int)  
    
  2. 複製上述架構,並將它貼到文本檔中。 將檔案儲存為CustOrderSchema.xml。

  3. 複製上述 Updategram,並將它貼到文字檔中。 將檔案儲存為上一個步驟中使用的相同資料夾中CustOrderUpdategram.xml。

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

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

這是對等的 XDR 架構:

<Schema xmlns="urn:schemas-microsoft-com:xml-data"  
        xmlns:sql="urn:schemas-microsoft-com:xml-sql">  
 <ElementType name="CustOrder" >  
    <AttributeType name="OrderID" />  
    <AttributeType name="EmployeeID" />  
    <AttributeType name="OrderType" default="1" />  
    <attribute type="OrderID"  />  
    <attribute type="EmployeeID" />  
    <attribute type="OrderType" />  
  </ElementType>  
</Schema>  

G. 使用 xsi:nil 屬性在數據行中插入 Null 值

如果您想要在數據表中對應的數據行中插入 Null 值,您可以在 updategram 中的元素上指定 xsi:nil 屬性。 在對應的 XSD 架構中,也必須指定 XSD nillable 屬性。

例如,請考慮此 XSD 架構:

<?xml version="1.0"?>  
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
<xsd:element name="Student" sql:relation="Students">  
  <xsd:complexType>  
    <xsd:all>  
      <xsd:element name="fname" sql:field="first_name"   
                                type="xsd:string"   
                                 nillable="true"/>  
    </xsd:all>  
    <xsd:attribute name="SID"   
                        sql:field="StudentID"  
                        type="xsd:ID"/>      
    <xsd:attribute name="lname"       
                        sql:field="last_name"  
                        type="xsd:string"/>  
    <xsd:attribute name="minitial"    
                        sql:field="middle_initial"   
                        type="xsd:string"/>  
    <xsd:attribute name="years"       
                         sql:field="no_of_years"  
                         type="xsd:integer"/>  
  </xsd:complexType>  
 </xsd:element>  
</xsd:schema>  

XSD 架構會指定 fname> 元素的 <nillable=“true”。 下列 Updategram 會使用此架構:

<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql"  
      xmlns:updg="urn:schemas-microsoft-com:xml-updategram"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  
<updg:sync mapping-schema='StudentSchema.xml'>  
  <updg:before/>  
  <updg:after>  
    <Student SID="S00004" lname="Elmaci" minitial="" years="2">  
      <fname xsi:nil="true">  
    </fname>  
    </Student>  
  </updg:after>  
</updg:sync>  
  
</ROOT>  

updategram 會針對< after> 區塊中的< fname> 元素指定 xsi:nil。 因此,執行此更新報時,數據表中first_name數據行會插入NULL的值。

測試 updategram
  1. tempdb 資料庫中建立下表:

    USE tempdb  
    CREATE TABLE Students (  
       StudentID char(6)NOT NULL ,  
       first_name varchar(50),  
       last_name varchar(50),  
       middle_initial char(1),  
       no_of_years int NULL)  
    GO  
    
  2. 複製上述架構,並將它貼到文本檔中。 將檔案儲存為StudentSchema.xml。

  3. 複製上述 Updategram,並將它貼到文字檔中。 將檔案儲存為上一個步驟中所使用的相同資料夾中的StudentUpdategram.xml,以儲存StudentSchema.xml。

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

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

H. 在 updategram 中指定命名空間

在 updategram 中,您可以擁有屬於 updategram 中相同元素中宣告之命名空間的專案。 在此情況下,對應的架構也必須宣告相同的命名空間,而且專案必須屬於該目標命名空間。

例如,在下列 updategram (UpdateGram-ElementHavingNamespace.xml), <Order> 元素屬於 元素中宣告的命名空間。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
  <updg:sync mapping-schema='XSD-ElementHavingNameSpace.xml'>  
    <updg:after>  
       <x:Order  xmlns:x="https://server/xyz/schemas/"  
             updg:at-identity="SalesOrderID"   
             RevisionNumber="1"  
             OrderDate="2001-07-01 00:00:00.000"  
             DueDate="2001-07-13 00:00:00.000"  
             OnlineOrderFlag="0"  
             CustomerID="676"  
             ContactID="378"  
             BillToAddressID="985"  
             ShipToAddressID="985"  
             ShipMethodID="5"  
             SubTotal="24643.9362"  
             TaxAmt="1971.5149"  
             Freight="616.0984"  
             rowguid="00009999-8888-7777-6666-554433221100"  
             ModifiedDate="2001-07-08 00:00:00.000" />  
    </updg:after>  
  </updg:sync>  
</ROOT>  

在此情況下,架構也必須宣告 命名空間,如下列架構所示:

下列架構 (XSD-ElementHavingNamespace.xml) 示範如何宣告對應的項目和屬性。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
     xmlns:dt="urn:schemas-microsoft-com:datatypes"   
     xmlns:sql="urn:schemas-microsoft-com:mapping-schema"    
     xmlns:x="https://server/xyz/schemas/"   
     targetNamespace="https://server/xyz/schemas/" >  
  <xsd:element name="Order" sql:relation="Sales.SalesOrderHeader" type="x:Order_type"/>  
  <xsd:complexType name="Order_type">  
    <xsd:attribute name="SalesOrderID"  type="xsd:ID"/>  
    <xsd:attribute name="RevisionNumber" type="xsd:unsignedByte"/>  
    <xsd:attribute name="OrderDate" type="xsd:dateTime"/>  
    <xsd:attribute name="DueDate" type="xsd:dateTime"/>  
    <xsd:attribute name="ShipDate" type="xsd:dateTime"/>  
    <xsd:attribute name="Status" type="xsd:unsignedByte"/>  
    <xsd:attribute name="OnlineOrderFlag" type="xsd:boolean"/>  
    <xsd:attribute name="SalesOrderNumber" type="xsd:string"/>  
    <xsd:attribute name="PurchaseOrderNumber" type="xsd:string"/>  
    <xsd:attribute name="AccountNumber" type="xsd:string"/>  
    <xsd:attribute name="CustomerID" type="xsd:int"/>  
    <xsd:attribute name="ContactID" type="xsd:int"/>  
    <xsd:attribute name="SalesPersonID" type="xsd:int"/>  
    <xsd:attribute name="TerritoryID" type="xsd:int"/>  
    <xsd:attribute name="BillToAddressID" type="xsd:int"/>  
    <xsd:attribute name="ShipToAddressID" type="xsd:int"/>  
    <xsd:attribute name="ShipMethodID" type="xsd:int"/>  
    <xsd:attribute name="CreditCardID" type="xsd:int"/>  
    <xsd:attribute name="CreditCardApprovalCode" type="xsd:string"/>  
    <xsd:attribute name="CurrencyRateID" type="xsd:int"/>  
    <xsd:attribute name="SubTotal" type="xsd:decimal"/>  
    <xsd:attribute name="TaxAmt" type="xsd:decimal"/>  
    <xsd:attribute name="Freight" type="xsd:decimal"/>  
    <xsd:attribute name="TotalDue" type="xsd:decimal"/>  
    <xsd:attribute name="Comment" type="xsd:string"/>  
    <xsd:attribute name="rowguid" type="xsd:string"/>  
    <xsd:attribute name="ModifiedDate" type="xsd:dateTime"/>  
  </xsd:complexType>  
</xsd:schema>  
測試 updategram
  1. 複製上述架構,並將它貼到文本檔中。 將檔案儲存為XSD-ElementHavingNamespace.xml。

  2. 複製上述 Updategram,並將它貼到文字檔中。 將檔案儲存為用來儲存XSD-ElementHavingnamespace.xml的相同資料夾中的Updategram-ElementHavingNamespace.xml。

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

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

I. 將數據插入 XML 資料類型資料列

XML 數據類型是在 SQL Server 2005 (9.x) 中引進的。 您可以使用 updategram,以下列布建插入並更新儲存在 xml 資料類型資料列中的資料:

  • xml 資料行無法用於識別現有的數據列。 因此,它不能包含在 updategram 的 updg:before 區段中。

  • 插入 xml 資料行之 XML 片段範圍內的命名空間將會保留,而且其命名空間宣告會加入插入片段的最上層元素。

例如,在下列 updategram (SampleUpdateGram.xml),<Desc> 元素會更新範例資料庫中 Production>productModel 數據表中的 AdventureWorks2022 ProductDescription 數據行。 此更新報的結果是 ProductDescription 數據行的 XML 內容會以 Desc> 元素的 <XML 內容進行更新。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
    <updg:sync mapping-schema="SampleSchema.xml" >  
       <updg:before>  
<ProductModel ProductModelID="19">  
   <Name>Mountain-100</Name>  
</ProductModel>  
    </updg:before>  
    <updg:after>  
 <ProductModel>  
    <Name>Mountain-100</Name>  
    <Desc><?xml-stylesheet href="ProductDescription.xsl" type="text/xsl"?>  
        <p1:ProductDescription xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription"   
              xmlns:wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain"   
              xmlns:wf="https://www.adventure-works.com/schemas/OtherFeatures"   
              xmlns:html="http://www.w3.org/1999/xhtml"   
              xmlns="">  
  <p1:Summary>  
     <html:p>Insert Example</html:p>  
  </p1:Summary>  
  <p1:Manufacturer>  
    <p1:Name>AdventureWorks</p1:Name>  
    <p1:Copyright>2002</p1:Copyright>  
    <p1:ProductURL>HTTP://www.Adventure-works.com</p1:ProductURL>  
  </p1:Manufacturer>  
  <p1:Features>These are the product highlights.   
    <wm:Warranty>  
       <wm:WarrantyPeriod>3 years</wm:WarrantyPeriod>  
       <wm:Description>parts and labor</wm:Description>  
    </wm:Warranty>  
    <wm:Maintenance>  
       <wm:NoOfYears>10 years</wm:NoOfYears>  
       <wm:Description>maintenance contract available through your dealer or any AdventureWorks retail store.</wm:Description>  
    </wm:Maintenance>  
    <wf:wheel>High performance wheels.</wf:wheel>  
    <wf:saddle>  
      <html:i>Anatomic design</html:i> and made from durable leather for a full-day of riding in comfort.</wf:saddle>  
    <wf:pedal>  
       <html:b>Top-of-the-line</html:b> clipless pedals with adjustable tension.</wf:pedal>  
    <wf:BikeFrame>Each frame is hand-crafted in our Bothell facility to the optimum diameter and wall-thickness required of a premium mountain frame. The heat-treated welded aluminum frame has a larger diameter tube that absorbs the bumps.</wf:BikeFrame>  
    <wf:crankset> Triple crankset; aluminum crank arm; flawless shifting. </wf:crankset>  
   </p1:Features>  
   <p1:Picture>  
      <p1:Angle>front</p1:Angle>  
      <p1:Size>small</p1:Size>  
      <p1:ProductPhotoID>118</p1:ProductPhotoID>  
   </p1:Picture>  
   <p1:Specifications> These are the product specifications.  
     <Material>Almuminum Alloy</Material>  
     <Color>Available in most colors</Color>  
     <ProductLine>Mountain bike</ProductLine>  
     <Style>Unisex</Style>  
     <RiderExperience>Advanced to Professional riders</RiderExperience>  
   </p1:Specifications>  
  </p1:ProductDescription>  
 </Desc>  
      </ProductModel>  
    </updg:after>  
  </updg:sync>  
</ROOT>  

updategram 指的是下列批注式 XSD 架構(SampleSchema.xml)。

<?xml version="1.0" encoding="utf-8" ?>  
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
           xmlns:sql="urn:schemas-microsoft-com:mapping-schema"  
           xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">   
  <xsd:element name="ProductModel"  sql:relation="Production.ProductModel" >  
     <xsd:complexType>  
       <xsd:sequence>  
          <xsd:element name="Name" type="xsd:string"></xsd:element>  
          <xsd:element name="Desc" sql:field="CatalogDescription" sql:datatype="xml">  
           <xsd:complexType>  
            <xsd:sequence>  
              <xsd:element name="ProductDescription">  
                 <xsd:complexType>  
                   <xsd:sequence>  
                     <xsd:element name="Summary" type="xsd:anyType">  
                     </xsd:element>  
                   </xsd:sequence>  
                 </xsd:complexType>  
              </xsd:element>  
            </xsd:sequence>  
           </xsd:complexType>  
          </xsd:element>   
       </xsd:sequence>  
       <xsd:attribute name="ProductModelID" sql:field="ProductModelID"/>  
     </xsd:complexType>  
  </xsd:element>  
</xsd:schema>  
測試 updategram
  1. 複製上述架構,並將它貼到文本檔中。 將檔案儲存為XSD-SampleSchema.xml。

    注意

    因為 updategram 支援預設對應,因此無法識別 xml 數據類型的開頭和結尾。 這實際上表示在插入或更新具有 xml 數據類型數據行的數據表時,需要對應架構。 如果未提供架構,SQLXML 會傳回錯誤,指出數據表中遺漏其中一個數據行。

  2. 複製上述 Updategram,並將它貼到文字檔中。 將檔案儲存為用來儲存SampleSchema.xml的相同資料夾中SampleUpdategram.xml。

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

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

另請參閱

Updategram 安全性考慮 (SQLXML 4.0)