--- title: "Identify key columns using sql:key-fields (SQLXML)" description: "Learn how to ensure proper nesting in an SQLXML 4.0 query result by specifying the sql:key-fields annotation in an XPath query to identify key columns." author: MikeRayMSFT ms.author: mikeray ms.date: "03/16/2017" ms.prod: sql ms.prod_service: "database-engine, sql-database" ms.technology: xml ms.topic: "reference" ms.custom: "seo-lt-2019" helpviewer_keywords: - "nesting XML results" - "proper nesting in results [SQLXML]" ms.assetid: 1a5ad868-8602-45c4-913d-6fbb837eebb0 monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current" --- # Identifying Key Columns Using sql:key-fields (SQLXML 4.0) [!INCLUDE [SQL Server Azure SQL Database](../../includes/applies-to-version/sql-asdb.md)] When an XPath query is specified against an XSD schema, key information is required in most cases to obtain proper nesting in the result. Specifying the **sql:key-fields** annotation is a way of ensuring that the appropriate hierarchy is generated. > [!NOTE] > To ensure proper nesting, it is recommended that you specify **sql:key-fields** for elements that map to tables. The XML produced is sensitive to the ordering of the underlying result set. If **sql:key-fields** is not specified, the XML generated might not be formed properly. The value of **sql:key-fields** identifies the column(s) that uniquely identify the rows in the relation. If more than one column is required to uniquely identify a row, the column values are delimited by spaces. You must use the **sql:key-fields** annotation when an element contains a **\** that is defined between the element and a child element but does not provide the primary key of the table that is specified in the parent element. ## Examples To create working samples using the following examples, you must meet certain requirements. For more information, see [Requirements for Running SQLXML Examples](../../relational-databases/sqlxml/requirements-for-running-sqlxml-examples.md). ### A. Producing the appropriate nesting when \ does not provide sufficient information This example shows where **sql:key-fields** must be specified. Consider the following schema. The schema specifies a hierarchy between the **\** and **\** elements in which the **\** element is the parent and the **\** element is a child. The **\** tag is used to specify the parent-child relationship. It identifies CustomerID in the Sales.SalesOrderHeader table as the parent key that refers to the CustomerID child key in the Sales.Customer table. The information provided in **\** is not sufficient to uniquely identify rows in the parent table (Sales.SalesOrderHeader). Therefore, without the **sql:key-fields** annotation, the hierarchy that is generated is inaccurate. With **sql:key-fields** specified on **\**, the annotation uniquely identifies the rows in the parent (Sales.SalesOrderHeader table), and its child elements appear below its parent. This is the schema: ``` ``` ##### To create a working sample of this schema 1. Copy the schema code above and paste it into a text file. Save the file as KeyFields1.xml. 2. Copy the following template and paste it into a text file. Save the file as KeyFields1T.xml in the same directory where you saved KeyFields1.xml. The XPath query in the template returns all the **\** elements with a CustomerID of less than 3. ``` /Order[@CustomerID < 3] ``` The directory path specified for the mapping schema (KeyFields1.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example: ``` mapping-schema="C:\MyDir\KeyFields1.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 Queries](../../relational-databases/sqlxml/using-ado-to-execute-sqlxml-4-0-queries.md). This is the partial result set: ``` ..... ``` ### B. Specifying sql:key-fields to produce proper nesting in the result In the following schema, there is no hierarchy specified using **\**. The schema still requires specifying the **sql:key-fields** annotation to uniquely identify employees in the HumanResources.Employee table. ``` ``` ##### To create a working sample of this schema 1. Copy the schema code above and paste it into a text file. Save the file as KeyFields2.xml. 2. Copy the following template and paste it into a text file. Save the file as KeyFields2T.xml in the same directory where you saved KeyFields2.xml. The XPath query in the template returns all the **\** elements: ``` /HumanResources.Employee ``` The directory path specified for the mapping schema (KeyFields2.xml) is relative to the directory where the template is saved. An absolute path also can be specified, for example: ``` mapping-schema="C:\MyDir\KeyFields2.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 Queries](../../relational-databases/sqlxml/using-ado-to-execute-sqlxml-4-0-queries.md). This is the result: ``` Production Technician - WC60 Marketing Assistant Engineering Manager ... ```