---
title: "Example: Specifying the ID and IDREF Directives | Microsoft Docs"
ms.custom: ""
ms.date: "03/01/2017"
ms.prod: sql
ms.prod_service: "database-engine"
ms.reviewer: ""
ms.technology: xml
ms.topic: conceptual
helpviewer_keywords:
- "IDREF directive"
- "ID directive"
ms.assetid: 7ff1ea73-71ca-4786-bd42-564f1b5de2d9
author: MightyPen
ms.author: genemi
---
# Example: Specifying the ID and IDREF Directives
[!INCLUDE[appliesto-ss-asdb-xxxx-xxx-md](../../includes/appliesto-ss-asdb-xxxx-xxx-md.md)]
This example is almost the same the [Specifying the ELEMENTXSINIL Directive](../../relational-databases/xml/example-specifying-the-elementxsinil-directive.md) example. The only difference is that the query specifies the **ID** and **IDREF** directives. These directives overwrite the types of the **SalesPersonID** attribute in the <`OrderHeader`> and <`OrderDetail`> elements. This forms intra-document links. You need the schema to see the overwritten types. Therefore, the query specifies the **XMLDATA** option in the FOR XML clause to retrieve the schema.
```sql
USE AdventureWorks2012;
GO
SELECT 1 as Tag,
0 as Parent,
SalesOrderID as [OrderHeader!1!SalesOrderID!id],
OrderDate as [OrderHeader!1!OrderDate],
CustomerID as [OrderHeader!1!CustomerID],
NULL as [SalesPerson!2!SalesPersonID],
NULL as [OrderDetail!3!SalesOrderID!idref],
NULL as [OrderDetail!3!LineTotal],
NULL as [OrderDetail!3!ProductID],
NULL as [OrderDetail!3!OrderQty]
FROM Sales.SalesOrderHeader
WHERE SalesOrderID=43659 or SalesOrderID=43661
UNION ALL
SELECT 2 as Tag,
1 as Parent,
SalesOrderID,
NULL,
NULL,
SalesPersonID,
NULL,
NULL,
NULL,
NULL
FROM Sales.SalesOrderHeader
WHERE SalesOrderID=43659 or SalesOrderID=43661
UNION ALL
SELECT 3 as Tag,
1 as Parent,
SOD.SalesOrderID,
NULL,
NULL,
SalesPersonID,
SOH.SalesOrderID,
LineTotal,
ProductID,
OrderQty
FROM Sales.SalesOrderHeader SOH,
Sales.SalesOrderDetail SOD
WHERE SOH.SalesOrderID = SOD.SalesOrderID
AND (SOH.SalesOrderID=43659 or SOH.SalesOrderID=43661)
ORDER BY [OrderHeader!1!SalesOrderID!id],
[SalesPerson!2!SalesPersonID],
[OrderDetail!3!SalesOrderID!idref],
[OrderDetail!3!LineTotal]
FOR XML EXPLICIT, XMLDATA;
```
This is the partial result. In the schema, note that the **ID** and **IDREF** directives have overwritten the data types of the **SalesOrderID** attribute in the <`OrderHeader`> and <`OrderDetail`> elements. If you remove these directives, the schema returns original types of these attributes.
```xml
...
...
```
## See Also
[Use EXPLICIT Mode with FOR XML](../../relational-databases/xml/use-explicit-mode-with-for-xml.md)