--- title: "XQueries Involving Order | Microsoft Docs" ms.custom: "" ms.date: "03/06/2017" ms.prod: sql ms.prod_service: sql ms.reviewer: "" ms.technology: xml ms.topic: "language-reference" dev_langs: - "XML" helpviewer_keywords: - "sequence [XQuery]" - "XQuery, sequence" - "ordered expressions [XQuery]" ms.assetid: 4f1266c5-93d7-402d-94ed-43f69494c04b author: "rothja" ms.author: "jroth" --- # XQueries Involving Order [!INCLUDE[tsql-appliesto-ss2012-xxxx-xxxx-xxx-md](../includes/tsql-appliesto-ss2012-xxxx-xxxx-xxx-md.md)] Relational databases do not have a concept of sequence. For example, you cannot make a request such as "Get the first customer from the database." However, you can query an XML document and retrieve the first \ element. Then, you will always retrieve the same customer. This topic illustrates queries based on the sequence in which nodes appear in the document. ## Examples ### A. Retrieve manufacturing steps at the second work center location for a product For a specific product model, the following query retrieves manufacturing steps at the second work center location in a sequence of work center locations in the manufacturing process. ```sql SELECT Instructions.query(' declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"; { (//AWMI:root/AWMI:Location)[2]/@* } { for $s in (//AWMI:root/AWMI:Location)[2]//AWMI:step return { string($s) } } ') as Result FROM Production.ProductModel WHERE ProductModelID=7 ``` Note the following from the previous query: - The expressions in the curly braces are replaced by the result of its evaluation. For more information, see [XML Construction (XQuery)](../xquery/xml-construction-xquery.md). - **@\*** retrieves all the attributes of the second work center location. - The FLWOR iteration (FOR ... RETURN) retrieves all the <`step`> child elements of the second work center location. - The [sql:column() function (XQuery)](../xquery/xquery-extension-functions-sql-column.md) includes the relational value in the XML that is being constructed. This is the result: ``` Assemble all frame components following blueprint 1299. ... ``` The previous query retrieves just the text nodes. If you want the whole <`step`> element returned instead, remove the **string()** function from the query: ### B. Find all the material and tools used at the second work center location in the manufacturing of a product For a specific product model, the following query retrieves the tools and materials used at the second work center location in the sequence of work center locations in the manufacturing process. ```sql SELECT Instructions.query(' declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"; { (//AWMI:root/AWMI:Location)[1]/@* } { for $s in (//AWMI:root/AWMI:Location)[1]//AWMI:step//AWMI:tool return { string($s) } } { for $s in (//AWMI:root/AWMI:Location)[1]//AWMI:step//AWMI:material return { string($s) } } ') as Result FROM Production.ProductModel where ProductModelID=7 ``` Note the following from the previous query: - The query constructs the element and retrieves its attribute values from the database. - It uses two FLWOR (for...return) iterations: one to retrieve tools and one to retrieve the material used. This is the result: ```xml T-85A framing tool Trim Jig TJ-26 router with a carbide tip 15 Forming Tool FT-15 aluminum sheet MS-2341 ``` ### C. Retrieve the first two product feature descriptions from the product catalog For a specific product model, the query retrieves the first two feature descriptions from the <`Features`> element in the product model catalog. ```sql SELECT CatalogDescription.query(' declare namespace p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription"; { for $F in /p1:ProductDescription/p1:Features return $F/*[position() <= 2] } ') as x FROM Production.ProductModel where ProductModelID=19 ``` Note the following from the previous query: The query body constructs XML that includes the <`ProductModel`> element that has the ProductModelID and ProductModelName attributes. - The query uses a FOR ... RETURN loop to retrieve the product model feature descriptions. The **position()** function is used to retrieve the first two features. This is the result: ```xml 3 year parts and labor 10 maintenance contact available through your dealer or any AdventureWorks retail store. ``` ### D. Find the first two tools used at the first work center location in the manufacturing process of the product For a product model, this query returns the first two tools used at the first work center location in the sequence of work center locations in the manufacturing process. The query is specified against the manufacturing instructions stored in the **Instructions** column of the **Production.ProductModel** table. ```sql SELECT Instructions.query(' declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"; for $Inst in (//AWMI:root/AWMI:Location)[1] return { $Inst/@* } { for $s in ($Inst//AWMI:step//AWMI:tool)[position() <= 2] return { string($s) } } ') as Result FROM Production.ProductModel where ProductModelID=7 ``` This is the result: ```xml T-85A framing tool Trim Jig TJ-26 ``` ### E. Find the last two manufacturing steps at the first work center location in the manufacturing of a specific product The query uses the **last()** function to retrieve the last two manufacturing steps. ```sql SELECT Instructions.query(' declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"; { (/AWMI:root/AWMI:Location)[1]/AWMI:step[(last()-1)]/text() } { (/AWMI:root/AWMI:Location)[1]/AWMI:step[last()]/text() } ') as Result FROM Production.ProductModel where ProductModelID=7 ``` This is the result: ```xml When finished, inspect the forms for defects per Inspection Specification . Remove the frames from the tool and place them in the Completed or Rejected bin as appropriate. ``` ## See Also [XML Data (SQL Server)](../relational-databases/xml/xml-data-sql-server.md) [XQuery Language Reference (SQL Server)](../xquery/xquery-language-reference-sql-server.md) [XML Construction (XQuery)](../xquery/xml-construction-xquery.md)