XQuery Language Reference (SQL Server)
Applies to: SQL Server
Transact-SQL supports a subset of the XQuery language that is used for querying the xml data type. This XQuery implementation is aligned with the July 2004 Working Draft of XQuery. The language is under development by the World Wide Web Consortium (W3C), with the participation of all major database vendors and also Microsoft. Because the W3C specifications may undergo future revisions before becoming a W3C recommendation, this implementation may be different from the final recommendation. This topic outlines the semantics and syntax of the subset of XQuery that is supported in SQL Server.
For more information, see the W3C XQuery 1.0 Language Specification.
XQuery is a language that can query structured or semi-structured XML data. With the xml data type support provided in the Database Engine, documents can be stored in a database and then queried by using XQuery.
XQuery is based on the existing XPath query language, with support added for better iteration, better sorting results, and the ability to construct the necessary XML. XQuery operates on the XQuery Data Model. This is an abstraction of XML documents, and the XQuery results that can be typed or untyped. The type information is based on the types provided by the W3C XML Schema language. If no typing information is available, XQuery handles the data as untyped. This is similar to how XPath version 1.0 handles XML.
To query an XML instance stored in a variable or column of xml type, you use the xml Data Type Methods. For example, you can declare a variable of xml type and query it by using the query() method of the xml data type.
DECLARE @x xml SET @x = '<ROOT><a>111</a></ROOT>' SELECT @x.query('/ROOT/a')
In the following example, the query is specified against the Instructions column of xml type in ProductModel table in the AdventureWorks database.
SELECT Instructions.query('declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"; /AWMI:root/AWMI:Location[@LocationID=10] ') as Result FROM Production.ProductModel WHERE ProductModelID=7
The XQuery includes the namespace declaration,
declare namespace``AWMI=..., and the query expression,
Note that the XQuery is specified against the Instructions column of xml type. The query() method of the xml data type is used to specify the XQuery.
The following table lists the related topics that can help in understanding the implementation of XQuery in the Database Engine.
|XML Data (SQL Server)||Explains the support for the xmldata type in the Database Engine and the methods you can use against this data type. The xml data type forms the input XQuery data model on which the XQuery expressions are executed.|
|XML Schema Collections (SQL Server)||Describes how the XML instances stored in a database can be typed. This means you can associate an XML schema collection with the xml type column. All the instances stored in the column are validated and typed against the schema in the collection and provide the type information for XQuery.|
The organization of this section is based on the World Wide Web Consortium (W3C) XQuery working draft specification. Some of the diagrams provided in this section are taken from that specification. This section compares the Microsoft XQuery implementation to the W3C specification, describes how Microsoft XQuery is different from the W3C and indicates what W3C features are not supported. The W3C specification is available at http://www.w3.org/TR/2004/WD-xquery-20040723.
In This Section
|XQuery Basics||Provides a basic overview of XQuery concepts, and also the expression evaluation (static and dynamic context), atomization, effective Boolean value, XQuery type system, sequence type matching, and error handling.|
|XQuery Expressions||Describes XQuery primary expressions, path expressions, sequence expressions, arithmetic comparison and logical expressions, XQuery construction, FLWOR expression, conditional and quantified expressions, and various expressions on sequence types.|
|Modules and Prologs (XQuery)||Describes XQuery prolog.|
|XQuery Functions against the xml Data Type||Describes a list of the XQuery functions that are supported.|
|XQuery Operators Against the xml Data Type||Describes XQuery operators that are supported.|
|Additional Sample XQueries Against the xml Data Type||Provides additional XQuery samples.|
XML Data (SQL Server)
XML Schema Collections (SQL Server)
Examples of Bulk Import and Export of XML Documents (SQL Server)