使用 XML Updategram 插入数据 (SQLXML 4.0)

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>  

<在阻止之前>

对于插入作,可以省略前>块。< 如果未指定可选 mapping-schema 属性, <则 updategram 中指定的 ElementName> 将映射到数据库表,子元素或属性映射到表中的列。

<阻止后>

可以在块后>指定一个或多个记录<

<如果后面的>块未为特定列提供值,则 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 在 AdventureWorks2012 数据库中的 HumanResources.Employee 表中插入记录。

在此示例中,updategram 未指定映射架构。 因此,updategram 使用默认映射,其中元素名称映射到表名,属性或子元素映射到该表中的列。

HumanResources.Department 表的 AdventureWorks2012 架构对所有列施加“非 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 数据库。

此外,元素名称必须括在方括号([ ])中。 由于字符 [和] 在 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>  

“订单详细信息”表中的 UnitPrice 列的类型。money 若要应用适当的类型转换(从 string 类型转换为 money 类型),必须添加美元符号字符($)作为值的一部分。 如果 updategram 未指定映射架构,则会计算值的第一个字符 string 。 如果第一个字符是美元符号($),则应用相应的转换。

如果针对映射架构指定了 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.SalesOrderDetail> 元素中的 <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 为<后块中的 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 查询

一。 将数据插入 XML 数据类型列

数据类型 xml 是在 SQL Server 2005 中引入的。 可以使用 updategram 插入和更新存储在 xml 数据类型列中的数据,并具有以下预配:

  • xml 列不能用于标识现有行。 因此,它不能包含在 updategram 的节中 updg:before

  • 将保留插入到 xml 列中的 XML 片段范围内的命名空间,其命名空间声明将添加到插入片段的顶部元素。

例如,在以下 updategram(SampleUpdateGram.xml),<Desc> 元素更新 AdventureWorks2012 示例数据库中 Production>productModel 表中的 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; alumunim 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="https://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)