當記錄實例出現在區塊中 <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 屬性用來傳回系統在具有 -type 直欄的 IDENTITY表格中新增記錄時所產生的身分值。
updg:id 屬性
如果 updategram 只插入記錄,則 updategram 不需要屬性 updg:id 。 如需 的相關updg:id資訊,請參閱 使用 XML 更新gram 更新資料 (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 的更多範例,請參閱 在 Updategram 中指定批注對應架構 (SQLXML 4.0) 。
大部分的範例都會
AdventureWorks2025使用範例資料庫。 所有更新都會套用至此資料庫中的數據表。
A. 使用 updategram 插入記錄
此以屬性為中心的 updategram 會在資料庫的HumanResources.Employee表格中AdventureWorks2025插入記錄。
在此範例中,updategram 不會指定對應結構描述。 因此,updategram 會使用預設對應,其中元素名稱會對應至數據表名稱,而屬性或子項目會對應至該數據表中的數據行。
AdventureWorks2025 HumanResources.Department 數據表的架構會對所有數據行施加「非 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 查詢
複製先前的更新文件並將其貼到文字檔案中。 將檔案儲存為
MyUpdategram.xml。建立並使用 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 插入多筆記錄
此更新圖會將兩個新的班次記錄新增至 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 查詢
複製先前的更新文件並將其貼到文字檔案中。 將檔案儲存為
Updategram-AddShifts.xml。建立並使用 SQLXML 4.0 測試指令碼 (
Sqlxml4test.vbs) 來執行範本。如需詳細資訊,請參閱使用 ADO 執行 SQLXML 4.0 查詢。
此範例的另一個版本是更新gram,它使用兩個單獨 <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 字元中無效,因為 XML 字元是有效的 SQL Server 識別碼,但無法使用 _xHHHH_ 有效的 XML 識別碼作為編碼值進行編碼,其中 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 查詢
複製先前的更新文件並將其貼到文字檔案中。 將檔案儲存為
UpdategramSpacesInTableName.xml。建立並使用 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 查詢
複製先前的更新文件並將其貼到文字檔案中。 將檔案儲存為
Updategram-returnId.xml。建立並使用 SQLXML 4.0 測試指令碼 (
Sqlxml4test.vbs) 來執行範本。如需詳細資訊,請參閱使用 ADO 執行 SQLXML 4.0 查詢。
E. 使用 updg:guid 屬性產生唯一值
在此範例中,updategram 會在 Cust 和 CustOrder 數據表中插入記錄。 此外,updategram 會使用 CustomerID updg:guid 屬性產生唯一值。
<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>
測試更新gram
複製先前的更新文件並將其貼到文字檔案中。 將檔案儲存為
Updategram-GenerateGuid.xml。建立這些資料表:
USE tempdb; CREATE TABLE Cust ( CustID UNIQUEIDENTIFIER, LastName VARCHAR (20) ); CREATE TABLE CustOrder ( CustID UNIQUEIDENTIFIER, OrderID INT );建立並使用 SQLXML 4.0 測試指令碼 (
Sqlxml4test.vbs) 來執行範本。如需詳細資訊,請參閱使用 ADO 執行 SQLXML 4.0 查詢。
F. 在 updategram 中指定結構描述
此範例中的 updategram 會將記錄插入下表:
CustOrder(OrderID, EmployeeID, OrderType)
XSD 結構描述會在此 updategram 中指定 (也就是說,沒有 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) 。
測試更新gram
在資料庫中
tempdb建立此資料表:USE tempdb; CREATE TABLE CustOrder ( OrderID INT, EmployeeID INT, OrderType INT );複製先前的結構描述並將其貼到文字檔中。 將檔案儲存為
CustOrderSchema.xml。複製先前的更新文件並將其貼到文字檔案中。 將檔案儲存在上一個步驟中使用的相同資料夾中。
CustOrderUpdategram.xml建立並使用 SQLXML 4.0 測試指令碼 (
Sqlxml4test.vbs) 來執行更新gram。如需詳細資訊,請參閱使用 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 屬性在直欄中插入空值
如果您想要在表格的對應直欄中插入空值,您可以在 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>元素。 因此,當執行此更新gram時,會為表格中的first_name欄插入值 。NULL
測試更新gram
在資料庫中
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複製先前的結構描述並將其貼到文字檔中。 將檔案儲存為
StudentSchema.xml。複製先前的更新文件並將其貼到文字檔案中。 將檔案儲存為上一個步驟中儲存的相同資料夾中,
StudentUpdategram.xml以儲存StudentSchema.xml。建立並使用 SQLXML 4.0 測試指令碼 (
Sqlxml4test.vbs) 來執行更新gram。如需詳細資訊,請參閱使用 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>
測試更新gram
複製先前的結構描述並將其貼到文字檔中。 將檔案儲存為
XSD-ElementHavingNamespace.xml。複製先前的更新文件並將其貼到文字檔案中。 將檔案儲存為用於儲存
Updategram-ElementHavingNamespace.xml的相同資料夾中。XSD-ElementHavingnamespace.xml建立並使用 SQLXML 4.0 測試指令碼 (
Sqlxml4test.vbs) 來執行更新gram。如需詳細資訊,請參閱使用 ADO 執行 SQLXML 4.0 查詢。
I. 將資料插入 XML 資料類型欄
您可以使用 updategram 來插入和更新儲存在 xml 資料類型直欄中的資料,但要考慮下列事項:
xml 資料行無法用來識別現有的資料列。 因此,它不能包含在更新圖的區段中
updg:before。插入 xml欄之 XML片段範圍內的命名空間會保留,其命名空間宣告會新增至插入片段的頂端元素。
例如,在下列 updategram ()SampleUpdateGram.xml 中,<Desc>元素會更新範例資料庫中>生產AdventureWorks2025productModel 資料表中的 ProductDescription 資料行。 此更新gram的結果是 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>
測試更新gram
複製先前的結構描述並將其貼到文字檔中。 將檔案儲存為
XSD-SampleSchema.xml。注意
由於 updategram 支援預設對應,因此無法識別 xml 資料類型的開頭和結尾。 這實際上表示在插入或更新具有 xml 數據類型數據行的數據表時,需要對應架構。 未提供結構描述時,SQLXML 會傳回錯誤,指出資料表中缺少其中一個資料行。
複製先前的更新文件並將其貼到文字檔案中。 將檔案儲存為用於儲存
SampleUpdategram.xml的相同資料夾中。SampleSchema.xml建立並使用 SQLXML 4.0 測試指令碼 (
Sqlxml4test.vbs) 來執行更新gram。如需詳細資訊,請參閱使用 ADO 執行 SQLXML 4.0 查詢。