---
title: "DiffGram Examples (SQLXML)"
ms.date: "03/04/2017"
ms.prod: sql
ms.prod_service: "database-engine, sql-database"
ms.reviewer: ""
ms.technology: xml
ms.topic: "reference"
helpviewer_keywords:
- "DiffGrams [SQLXML], examples"
- "examples [SQLXML], DiffGram"
- "diffgr:parentID"
- "parentID annotation"
ms.assetid: fc148583-dfd3-4efb-a413-f47b150b0975
author: MightyPen
ms.author: genemi
ms.custom: "seo-lt-2019"
monikerRange: "=azuresqldb-current||>=sql-server-2016||=sqlallproducts-allversions||>=sql-server-linux-2017||=azuresqldb-mi-current"
---
# DiffGram Examples (SQLXML 4.0)
[!INCLUDE[appliesto-ss-asdb-xxxx-xxx-md](../../../includes/appliesto-ss-asdb-xxxx-xxx-md.md)]
The examples in this topic consist of DiffGrams that perform insert, update, and delete operations to the database. Before using the examples, note the following:
- The examples use two tables (Cust and Ord) that must be created if you want to test the DiffGram examples:
```
Cust(CustomerID, CompanyName, ContactName)
Ord(OrderID, CustomerID)
```
- Most of the examples in this topic use the following XSD Schema:
```
Diffgram Customers/Orders Schema.
```
Save this schema as DiffGramSchema.xml in the same folder where you save other files used in the examples.
## A. Deleting a record by using a DiffGram
The DiffGram in this example deletes a customer (whose CustomerID is ALFKI) record from the Cust table and deletes the corresponding order record (whose OrderID is 1) from the Ord table.
```
Alfreds Futterkiste
Maria Anders
```
In the **\** block, there is an **\** element (**diffgr:id="Order1"**) and a **\** element (**diffgr:id="Customer1"**). These elements represent existing records in the database. The **\** element does not have the corresponding records (with the same **diffgr:id**). This indicates a delete operation.
#### To test the DiffGram
1. Create these tables in the **tempdb** database.
```
CREATE TABLE Cust(
CustomerID nchar(5) Primary Key,
CompanyName nvarchar(40) NOT NULL ,
ContactName nvarchar(60) NULL)
GO
CREATE TABLE Ord(
OrderID int Primary Key,
CustomerID nchar(5) Foreign Key REFERENCES Cust(CustomerID))
GO
```
2. Add this sample data:
```
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ALFKI', N'Alfreds Futterkiste', N'Maria Anders')
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ANATR', N'Ana Trujillo Emparedados y helados', N'Ana Trujillo')
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ANTON', N'Antonio Moreno Taquería', N'Antonio Moreno')
INSERT INTO Ord(OrderID, CustomerID) VALUES(1, N'ALFKI')
INSERT INTO Ord(OrderID, CustomerID) VALUES(2, N'ANATR')
INSERT INTO Ord(OrderID, CustomerID) VALUES(3, N'ANTON')
```
3. Copy the DiffGram above and paste it into a text file. Save the file as MyDiffGram.xml in the same folder used in the previous step.
4. Copy the DiffGramSchema provided earlier in this topic and paste it into a text file. Save the file as DiffGramSchema.xml.
5. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the DiffGram.
For more information, see [Using ADO to Execute SQLXML 4.0 Queries](../../../relational-databases/sqlxml/using-ado-to-execute-sqlxml-4-0-queries.md).
## B. Inserting a record by using a DiffGram
In this example, the DiffGram inserts a record in the Cust table and a record in the Ord table.
```
C3Company
C3Contact
```
In this DiffGram the **\** block is not specified (no existing database records identified). There are two record instances (identified by the **\** and **\** elements in the **\** block) that map to Cust and Ord tables, respectively. Both of these elements specify the **diffgr:hasChanges** attribute (**hasChanges="inserted"**). This indicates an insert operation. In this DiffGram, if you specify **hasChanges="modified"**, you are indicating that you want to modify a record that does not exist, which results in an error.
#### To test the DiffGram
1. Create these tables in the **tempdb** database.
```
CREATE TABLE Cust(
CustomerID nchar(5) Primary Key,
CompanyName nvarchar(40) NOT NULL ,
ContactName nvarchar(60) NULL)
GO
CREATE TABLE Ord(
OrderID int Primary Key,
CustomerID nchar(5) Foreign Key REFERENCES Cust(CustomerID))
GO
```
2. Add this sample data:
```
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ALFKI', N'Alfreds Futterkiste', N'Maria Anders')
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ANATR', N'Ana Trujillo Emparedados y helados', N'Ana Trujillo')
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ANTON', N'Antonio Moreno Taquería', N'Antonio Moreno')
INSERT INTO Ord(OrderID, CustomerID) VALUES(1, N'ALFKI')
INSERT INTO Ord(OrderID, CustomerID) VALUES(2, N'ANATR')
INSERT INTO Ord(OrderID, CustomerID) VALUES(3, N'ANTON')
```
3. Copy the DiffGram above and paste it into a text file. Save the file as MyDiffGram.xml in the same folder used in the previous step.
4. Copy the DiffGramSchema provided earlier in this topic and paste it into a text file. Save the file as DiffGramSchema.xml.
5. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the DiffGram.
For more information, see [Using ADO to Execute SQLXML 4.0 Queries](../../../relational-databases/sqlxml/using-ado-to-execute-sqlxml-4-0-queries.md).
## C. Updating an existing record by using a DiffGram
In this example, the DiffGram updates customer information (CompanyName and ContactName) for customer ALFKI.
```
Bottom Dollar Markets
Antonio Moreno
Alfreds Futterkiste
Maria Anders
```
The **\** block includes a **\** element (**diffgr:id="Customer1"**). The **\** block includes the corresponding **\** element with same **id**. The **\** element in the **\** also specifies **diffgr:hasChanges="modified"**. This indicates an update operation, and the customer record in the **Cust** table is updated accordingly. Note that if the **diffgr:hasChanges** attribute is not specified, the DiffGram processing logic ignores this element and no updates are performed.
#### To test the DiffGram
1. Create these tables in the **tempdb** database.
```
CREATE TABLE Cust(
CustomerID nchar(5) Primary Key,
CompanyName nvarchar(40) NOT NULL ,
ContactName nvarchar(60) NULL)
GO
CREATE TABLE Ord(
OrderID int Primary Key,
CustomerID nchar(5) Foreign Key REFERENCES Cust(CustomerID))
GO
```
2. Add this sample data:
```
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ALFKI', N'Alfreds Futterkiste', N'Maria Anders')
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ANATR', N'Ana Trujillo Emparedados y helados', N'Ana Trujillo')
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ANTON', N'Antonio Moreno Taquería', N'Antonio Moreno')
INSERT INTO Ord(OrderID, CustomerID) VALUES(1, N'ALFKI')
INSERT INTO Ord(OrderID, CustomerID) VALUES(2, N'ANATR')
INSERT INTO Ord(OrderID, CustomerID) VALUES(3, N'ANTON')
```
3. Copy the DiffGram above and paste it into a text file. Save the file as MyDiffGram.xml in the same folder used in the previous step.
4. Copy the DiffGramSchema provided earlier in this topic and paste it into a text file. Save the file as DiffGramSchema.xml.
5. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the DiffGram.
For more information, see [Using ADO to Execute SQLXML 4.0 Queries](../../../relational-databases/sqlxml/using-ado-to-execute-sqlxml-4-0-queries.md).
## D. Inserting, updating, and deleting records by using a DiffGram
In this example, a relatively complex DiffGram is used to perform insert, update, and delete operations.
```
Bottom Dollar Markets
Elizabeth Lincoln
Chop-suey Chinese
Yang Wang
Around the Horn
Thomas Hardy
Alfreds Futterkiste
Maria Anders
Ana Trujillo Emparedados y helados
Ana Trujillo
```
The DiffGram logic processes this DiffGram as follows:
- In accordance with DiffGram processing logic, all the top-level elements in the **\** block map to corresponding tables, as described in the mapping schema.
- The **\** block has an **\** element (**dffgr:id="Order1"**) and a **\** element (**diffgr:id="Customer1"**) for which there is no corresponding element in the **\** block (with the same ID). This indicates a delete operation, and the records are deleted from the Cust and Ord tables.
- The **\** block has a **\** element (**diffgr:id="Customer2"**) for which there is a corresponding **\** element in the **\** block (with the same ID). The element in the **\** block specifies **diffgr:hasChanges="modified"**. This is an update operation in which for customer ANATR, the CompanyName and ContactName information is updated in the Cust table using values that are specified in the **\** block.
- The **\** block has a **\** element (**diffgr:id="Customer3"**) and an **\** element (**diffgr:id="Order3"**). Neither of these elements specify the **diffgr:hasChanges** attribute. Therefore, the DiffGram processing logic ignores these elements.
- The **\** block has a **\** element (**diffgr:id="Customer4"**) and an **\** element (**diffgr:id="Order4"**) for which there are no corresponding elements in the \ block. These elements in the **\** block specify **diffgr:hasChanges="inserted"**. Therefore, a new record is added in the Cust table and in the Ord table.
#### To test the DiffGram
1. Create the following tables in the **tempdb** database.
```
CREATE TABLE Cust(
CustomerID nchar(5) Primary Key,
CompanyName nvarchar(40) NOT NULL ,
ContactName nvarchar(60) NULL)
GO
CREATE TABLE Ord(
OrderID int Primary Key,
CustomerID nchar(5) Foreign Key REFERENCES Cust(CustomerID))
GO
```
2. Add this sample data:
```
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ALFKI', N'Alfreds Futterkiste', N'Maria Anders')
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ANATR', N'Ana Trujillo Emparedados y helados', N'Ana Trujillo')
INSERT INTO Cust(CustomerID, CompanyName, ContactName) VALUES
(N'ANTON', N'Antonio Moreno Taquería', N'Antonio Moreno')
INSERT INTO Ord(OrderID, CustomerID) VALUES(1, N'ALFKI')
INSERT INTO Ord(OrderID, CustomerID) VALUES(2, N'ANATR')
INSERT INTO Ord(OrderID, CustomerID) VALUES(3, N'ANTON')
```
3. Copy the DiffGram above and paste it into a text file. Save the file as MyDiffGram.xml in the same folder used in the previous step.
4. Copy the DiffGramSchema provided earlier in this topic and paste it into a text file. Save the file as DiffGramSchema.xml.
5. Create and use the SQLXML 4.0 Test Script (Sqlxml4test.vbs) to execute the DiffGram.
For more information, see [Using ADO to Execute SQLXML 4.0 Queries](../../../relational-databases/sqlxml/using-ado-to-execute-sqlxml-4-0-queries.md).
## E. Applying updates by using a DiffGram with the diffgr:parentID annotation
This example illustrates how the **parentID** annotation that is specified in the **\** block of the DiffGram is used in applying the updates.
```
```
This DiffGram specifies a delete operation because there is only a **\** block. In the DiffGram, the **parentID** annotation is used to specify a parent-child relationship between the orders and order details. When SQLXML deletes the records, it deletes records from the child table that is identified by this relationship and then deletes the records from the corresponding parent table.