--- title: "Specifying an Annotated Mapping Schema in an Updategram (SQLXML 4.0) | Microsoft Docs" ms.custom: "" ms.date: "03/17/2017" ms.prod: "sql-server-2016" ms.reviewer: "" ms.suite: "" ms.technology: - "dbe-xml" ms.tgt_pltfrm: "" ms.topic: "reference" helpviewer_keywords: - "annotated XSD schemas, updategrams" - "data types [SQLXML], mapping schema in updategrams" - "updategrams [SQLXML], annotated mapping schemas" - "annotated XDR schemas, updategrams" - "inverse attribute" - "parent-child relationships [SQLXML]" - "mapping-schema attribute" - "mapping schema [SQLXML], updategrams" - "sql:inverse" ms.assetid: 2e266ed9-4cfb-434a-af55-d0839f64bb9a caps.latest.revision: 26 author: "douglaslMS" ms.author: "douglasl" manager: "jhubbard" --- # Specifying an Annotated Mapping Schema in an Updategram (SQLXML 4.0) This topic explains how the mapping schema (XSD or XDR) that is specified in an updategram is used to process the updates. In an updategram, you can provide the name of an annotated mapping schema to use in mapping the elements and attributes in the updategram to tables and columns in [!INCLUDE[msCoName](../../../includes/msconame-md.md)] [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)]. When a mapping schema is specified in an updategram, the element and attribute names that are specified in the updategram must map to the elements and attributes in the mapping schema. To specify a mapping schema, you use the **mapping-schema** attribute of the **\** element. The following examples show two updategrams: one that uses a simple mapping schema, and one that uses a more complex schema. > [!NOTE] > This documentation assumes that you are familiar with templates and mapping schema support in [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)]. For more information, see [Introduction to Annotated XSD Schemas (SQLXML 4.0)](../../../relational-databases/sqlxml/annotated-xsd-schemas/introduction-to-annotated-xsd-schemas-sqlxml-4-0.md). For legacy applications that use XDR, see [Annotated XDR Schemas (Deprecated in SQLXML 4.0)](../../../relational-databases/sqlxml/annotated-xsd-schemas/annotated-xdr-schemas-deprecated-in-sqlxml-4-0.md). ## Dealing with Data Types If the schema specifies the **image**, **binary**, or **varbinary**[!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] data type (by using **sql:datatype**) and does not specify an XML data type, the updategram assumes that the XML data type is **binary base 64**. If your data is **bin.base** type, you must explicitly specify the type (**dt:type=bin.base** or **type="xsd:hexBinary"**). If the schema specifies the **dateTime**, **date**, or **time** XSD data type, you must also specify the corresponding [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] data type by using **sql:datatype="dateTime"**. When handling parameters of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] **money** type, you must explicitly specify **sql:datatype="money"** on the appropriate node in the mapping schema. ## Examples To create working samples using the following examples, you must meet the requirements specified in [Requirements for Running SQLXML Examples](../../../relational-databases/sqlxml/requirements-for-running-sqlxml-examples.md). ### A. Creating an updategram with a simple mapping schema The following XSD schema (SampleSchema.xml) is a mapping schema that maps the **\** element to the Sales.Customer table: ``` ``` The following updategram inserts a record into the Sales.Customer table and relies on the previous mapping schema to properly map this data to the table. Notice that the updategram uses the same element name, **\**, as defined in the schema. This is mandatory because the updategram specifies a particular schema. ##### To test the updategram 1. Copy the schema code above and paste it into a text file. Save the file as SampleUpdateSchema.xml. 2. Copy the updategram template below and paste it into a text file. Save the file as SampleUpdategram.xml in the same directory where you saved SampleUpdateSchema.xml. ``` ``` The directory path specified for the mapping schema (SampleUpdateSchema.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example: ``` mapping-schema="C:\SqlXmlTest\SampleUpdateSchema.xml" ``` 3. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the template. For more information, see [Using ADO to Execute SQLXML 4.0 Queries](../../../relational-databases/sqlxml/using-ado-to-execute-sqlxml-4-0-queries.md). This is the equivalent XDR schema: ``` ``` ### B. Inserting a record by using the parent-child relationship specified in the mapping schema Schema elements can be related. The **\** element specifies the parent-child relationship between the schema elements. This information is used to update corresponding tables that have primary-key/foreign-key relationship. The following mapping schema (SampleSchema.xml) consists of two elements, **\** and **\**: ``` ``` The following updategram uses this XSD schema to add a new order detail record (an **\** element in the **\** block) for order 43860. The **mapping-schema** attribute is used to specify the mapping schema in the updategram. ``` ``` ##### To test the updategram 1. Copy the schema code above and paste it into a text file. Save the file as SampleUpdateSchema.xml. 2. Copy the updategram template above and paste it into a text file. Save the file as SampleUpdategram.xml in the same directory where you saved SampleUpdateSchema.xml. The directory path specified for the mapping schema (SampleUpdateSchema.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example: ``` mapping-schema="C:\SqlXmlTest\SampleUpdateSchema.xml" ``` 3. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the template. For more information, see [Using ADO to Execute SQLXML 4.0 Queries](../../../relational-databases/sqlxml/using-ado-to-execute-sqlxml-4-0-queries.md). This is the equivalent XDR schema: ``` ``` ### C. Inserting a record by using the parent-child relationship and inverse annotation specified in the XSD schema This example illustrates how the updategram logic uses the parent-child relationship specified in the XSD to process updates, and how the **inverse** annotation is used. For more information about the **inverse** annotation, see [Specifying the sql:inverse Attribute on sql:relationship (SQLXML 4.0)](../../../relational-databases/sqlxml-annotated-xsd-schemas-using/specifying-the-sql-inverse-attribute-on-sql-relationship-sqlxml-4-0.md). This example assumes that the following tables are in the **tempdb** database: - `Cust (CustomerID, CompanyName)`, where `CustomerID` is the primary key - `Ord (OrderID, CustomerID)`, where `CustomerID` is a foreign key that refers to the `CustomerID` primary key in the `Cust` table. The updategram uses the following XSD schema to insert records into the Cust and Ord tables : ``` ``` The XSD schema in this example has **\** and **\** elements, and it specifies a parent-child relationship between the two elements. It identifies **\** as the parent element and **\** as the child element. The updategram processing logic uses the information about the parent-child relationship to determine the order in which records are inserted into tables. In this example, the updategram logic first attempts to insert a record into the Ord table (because **\** is the parent) and then attempts to insert a record into the Cust table (because **\** is the child). However, because of the primary key/foreign key information that is contained in the database table schema, this insert operation causes a foreign key violation in the database and the insert fails. To instruct the updategram logic to reverse the parent-child relationship during the update operation, the **inverse** annotation is specified on the **\** element. As a result, records are added first in the Cust table and then in the Ord table, and the operation succeeds. The following updategram inserts an order (OrderID=2) in the Ord table and a customer (CustomerID='AAAAA') in the Cust table by using the specified XSD schema: ``` ``` ##### To test the updategram 1. Create these tables in the **tempdb** database: ``` USE tempdb CREATE TABLE Cust(CustomerID varchar(5) primary key, CompanyName varchar(20)) GO CREATE TABLE Ord (OrderID int primary key, CustomerID varchar(5) references Cust(CustomerID)) GO ``` 2. Copy the schema code above and paste it into a text file. Save the file as SampleUpdateSchema.xml. 3. Copy the updategram template above and paste it into a text file. Save the file as SampleUpdategram.xml in the same directory where you saved SampleUpdateSchema.xml. The directory path specified for the mapping schema (SampleUpdateSchema.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example: ``` mapping-schema="C:\SqlXmlTest\SampleUpdateSchema.xml" ``` 4. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the template. For more information, see [Using ADO to Execute SQLXML 4.0 Queries](../../../relational-databases/sqlxml/using-ado-to-execute-sqlxml-4-0-queries.md). ## See Also [Updategram Security Considerations (SQLXML 4.0)](../../../relational-databases/sqlxml-annotated-xsd-schemas-xpath-queries/security/updategram-security-considerations-sqlxml-4-0.md)