使用 XML 更新报插入数据 (SQLXML 4.0)

适用于:SQL ServerAzure SQL 数据库

updategram 指示当记录实例出现在块中 <after> 但不出现在相应 <before> 块中时插入作。 在这种情况下,updategram 会将块中的 <after> 记录插入数据库中。

以下是 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>

<before> 块

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

<after> 块

可以在块中 <after> 指定一个或多个记录。

<after>如果块未为特定列提供值,则 updategram 将使用批注架构中指定的默认值(如果已指定架构)。 如果架构未指定列的默认值,则 updategram 不会为此列指定任何显式值,而是为此列分配 SQL Server 默认值(如果指定)。 如果没有 SQL Server 默认值并且列接受值 NULL ,则 updategram 会将列值设置为 NULL。 如果列没有默认值或接受值 NULL ,该命令将失败,updategram 将返回错误。 可选 updg:returnid 属性用于返回在具有 IDENTITY-type 列的表中添加记录时由系统生成的标识值。

updg:id 属性

如果 updategram 仅插入记录,则 updategram 不需要属性 updg:id 。 有关详细信息updg:id,请参阅使用 XML Updategram 更新数据(SQLXML 4.0)。

updg:at-identity 属性

当 updategram 在具有 IDENTITY-type 列的表中插入记录时,updategram 可以使用可选 updg:at-identity 属性捕获系统分配的值。 然后,updategram 可以在后续操作中使用此值。 执行 updategram 后,可以返回通过指定 updg:returnid 属性生成的标识值。

updg:guid 属性

该 updg:guid 属性是生成全局唯一标识符的可选属性。 此值保留在指定其所在的整个 <sync> 块的范围内。 可以在块中的任何 <sync> 位置使用此值。 该属性调用 NEWGUID() SQL Server 函数以生成唯一标识符。

示例

若要使用以下示例创建工作示例,必须满足运行 SQLXML 示例的要求中指定的要求。

在使用 updategram 示例之前,请考虑:

  • 大多数示例使用默认映射(即,未在 updategram 中指定任何映射架构)。 有关使用映射架构的更新报的更多示例,请参阅在 Updategram 中指定带批注的映射架构(SQLXML 4.0)。

  • 大多数示例使用 AdventureWorks2025 示例数据库。 已对该数据库中的表应用所有更新。

A. 使用 updategram 插入记录

以属性为中心的 updategram 在数据库中的 HumanResources.Employee 表中 AdventureWorks2025 插入记录。

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

HumanResources.Department 表的 AdventureWorks2025 架构对所有列施加了“not null”限制。 所以,updategram 必须包括为所有列指定的值。 DepartmentID 是一个 IDENTITY-type 列。 因此,没有为其指定任何值。

<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 未指定可选 <before> 块。

<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,它使用两 <after> 个单独的块而不是一个块插入这两个员工。 这种做法是有效的,并且可以按照如下形式进行编码:

<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 数据库中的“订单详细信息”表。 但是,这在 XML 字符中无效,这些字符是有效的 SQL Server 标识符,但无效的 XML 标识符可以使用编码值进行编码 _xHHHH_ ,其中 HHHH 表示字符的四位数十六进制 UCS-2 代码(以最重要的位优先顺序表示)。

注意

此示例使用 Northwind 数据库。 可以使用可从此 Microsoft网站下载的 SQL 脚本安装 Northwind 数据库。

此外,元素名称必须括在方括号中([ ])。 由于字符 [和] 在 XML 中无效,因此必须将它们分别编码为 _x005B_ 和 _x005D_/ (如果使用映射架构,可以提供不包含无效字符的元素名称,例如空格。映射架构执行必要的映射;因此,无需对这些字符进行编码。

此 updategram 向 Northwind 数据库中的 Order Details 表添加一条记录:

<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 列是 货币 类型。 若要将适当的类型转换(从 字符串 类型应用到 货币 类型),必须添加美元符号字符($)作为值的一部分。 如果 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-type 列的值

以下 updategram 插入两条记录:一条记录在 Sales.SalesOrderHeader 表中,另一条记录在 Sales.SalesOrderDetail 表中。

首先,updategram 将记录添加到 Sales.SalesOrderHeader 表中。 在此表中,SalesOrderID 列是一个 IDENTITY-type 列。 因此,将此记录添加到表时,updategram 使用 at-identity 属性将分配的 SalesOrderID 值捕获为“x”(占位符值)。 然后,Updategram 将此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. 制作样本表。 连接到所需的样本或测试数据库,运行剩余的代码样本。

    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) 描述一个由<CustOrder>属性组成的OrderIDEmployeeID元素。 若要使架构更有趣,请将默认值分配给 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. 在所需的样本或测试数据库中创建该表。

    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 架构为nillable="true"元素指定<fname>。 以下 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 为xsi:nil块中的<fname>元素指定<after>。 因此,执行此更新报时,将为表中的first_name列插入一个值 NULL 。

测试 updategram

  1. 在目标样本或测试数据库中创建以下表格。

    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 并将其粘贴到文本文件中。 将文件另存为 Updategram-ElementHavingNamespace.xml 用于保存 XSD-ElementHavingnamespace.xml的同一文件夹中。

  3. 创建并使用 SQLXML 4.0 测试脚本(Sqlxml4test.vbs)执行 updategram。

    有关详细信息,请参阅使用 ADO 执行 SQLXML 4.0 查询。

I. 将数据插入 XML 数据类型列

可以使用 updategram 插入和更新存储在 xml 数据类型列中的数据,但有以下注意事项:

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

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

例如,在以下 updategram(SampleUpdateGram.xml)中 <Desc> ,元素更新示例数据库中 Production>productModel 表中 AdventureWorks2025 的 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 href="ProductDescription.xsl" type="text/xsl" e="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>Aluminum 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 并将其粘贴到文本文件中。 将文件另存为 SampleUpdategram.xml 用于保存 SampleSchema.xml的同一文件夹中。

  3. 创建并使用 SQLXML 4.0 测试脚本(Sqlxml4test.vbs)执行 updategram。

    有关详细信息,请参阅使用 ADO 执行 SQLXML 4.0 查询。