Xquery 语言参考 (SQL Server)XQuery Language Reference (SQL Server)

适用对象: 是SQL Server 否Azure SQL 数据库 否Azure Synapse Analytics (SQL DW) 否并行数据仓库 APPLIES TO: yesSQL Server noAzure SQL Database noAzure Synapse Analytics (SQL DW) noParallel Data Warehouse

Transact-SQLTransact-SQL 支持用于查询的 XQuery 语言的子集xml数据类型。supports a subset of the XQuery language that is used for querying the xml data type. 此 XQuery 实现符合 2004 年 7 月的 XQuery 工作草案。This XQuery implementation is aligned with the July 2004 Working Draft of XQuery. 该语言正在由 World Wide Web 联合会 (W3C) 开发,所有主要数据库供应商和 Microsoft 也参与此开发。The language is under development by the World Wide Web Consortium (W3C), with the participation of all major database vendors and also Microsoft. 由于 W3C 规范在成为 W3C 建议之前还可能进行修订,因此此实现可能与最终的建议有所不同。Because the W3C specifications may undergo future revisions before becoming a W3C recommendation, this implementation may be different from the final recommendation. 本主题概要介绍了 SQL ServerSQL Server 中所支持的 XQuery 子集的语义和语法。This topic outlines the semantics and syntax of the subset of XQuery that is supported in SQL ServerSQL Server.

有关详细信息,请参阅W3C XQuery 1.0 语言规范For more information, see the W3C XQuery 1.0 Language Specification.

XQuery 是一种可以查询结构化或半结构化 XML 数据的语言。XQuery is a language that can query structured or semi-structured XML data. xml数据类型中提供的支持数据库引擎Database Engine,文档可以存储在数据库,然后通过使用 XQuery 查询。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 基于现有的 XPath 查询语言,并支持更好的迭代、更好的排序结果以及构造必需的 XML 的功能。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 在 XQuery 数据模型上运行。XQuery operates on the XQuery Data Model. 此模型是 XML 文档以及可能为类型化也可能为非类型化的 XQuery 结果的抽象概念。This is an abstraction of XML documents, and the XQuery results that can be typed or untyped. 类型信息基于 W3C XML 架构语言所提供的类型。The type information is based on the types provided by the W3C XML Schema language. 如果没有可用的类型化信息,XQuery 将按照非类型化处理数据。If no typing information is available, XQuery handles the data as untyped. 这与 XPath 1.0 版处理 XML 的方式相似。This is similar to how XPath version 1.0 handles XML.

若要查询的变量或列中存储的 XML 实例xml类型,则使用xml 数据类型方法To query an XML instance stored in a variable or column of xml type, you use the xml Data Type Methods. 例如,可以声明的变量xml键入和使用查询query () 方法xml数据类型。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')  

在以下示例中,针对的 Instructions 列指定了查询xml AdventureWorks 数据库中 ProductModel 表中的类型。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";           
') as Result   
FROM  Production.ProductModel  
WHERE ProductModelID=7  

XQuery 包含命名空间声明declare namespace``AWMI=...,并查询表达式中, /AWMI:root/AWMI:Location[@LocationID=10]The XQuery includes the namespace declaration, declare namespace``AWMI=..., and the query expression, /AWMI:root/AWMI:Location[@LocationID=10].

请注意,针对的 Instructions 列指定 XQuery xml类型。Note that the XQuery is specified against the Instructions column of xml type. XML 数据类型的 query() 方法用于指定 XQuery。The query() method of the xml data type is used to specify the XQuery.

下表列出了有助于理解如何在 数据库引擎Database Engine中实现 XQuery 的相关主题。The following table lists the related topics that can help in understanding the implementation of XQuery in the 数据库引擎Database Engine.

主题Topic 描述Description
XML 数据 (SQL Server)XML Data (SQL Server) 介绍为支持xml中的数据类型数据库引擎Database Engine以及可以对此数据类型使用的方法。Explains the support for the xmldata type in the 数据库引擎Database Engine and the methods you can use against this data type. Xml数据类型的形成输入的 XQuery 数据模型执行的 XQuery 表达式。The xml data type forms the input XQuery data model on which the XQuery expressions are executed.
XML 架构集合 (SQL Server)XML Schema Collections (SQL Server) 说明如何类型化数据库中存储的 XML 实例。Describes how the XML instances stored in a database can be typed. 这意味着可以将 XML 架构集合与相关联xml类型列。This means you can associate an XML schema collection with the xml type column. 列中存储的所有实例根据集合中的架构进行验证和类型化,并为 XQuery 提供类型信息。All the instances stored in the column are validated and typed against the schema in the collection and provide the type information for XQuery.


本部分的组织结构基于 World Wide Web 联合会 (W3C) 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. 本部分将 Microsoft XQuery 实现与 W3C 规范进行比较,介绍 Microsoft XQuery 与 W3C 的不同之处并指明不支持的 W3C 功能。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. W3C 规范位于 http://www.w3.org/TR/2004/WD-xquery-20040723 The W3C specification is available at http://www.w3.org/TR/2004/WD-xquery-20040723.

本节内容In This Section

主题Topic 描述Description
XQuery 基础知识XQuery Basics 简要介绍了 XQuery 概念,以及表达式计算(静态和动态上下文)、原子化、有效的布尔值、XQuery 类型系统、序列类型匹配和错误处理。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 表达式XQuery Expressions 介绍 XQuery 主表达式、路径表达式、序列表达式、算术比较和逻辑表达式、XQuery 构造、FLWOR 表达式、条件和定量表达式以及序列类型的各种表达式。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.
模块和 Prolog (XQuery)Modules and Prologs (XQuery) 介绍 XQuery prolog。Describes XQuery prolog.
针对 xml 数据类型的 XQuery 函数XQuery Functions against the xml Data Type 介绍支持的一系列 XQuery 函数。Describes a list of the XQuery functions that are supported.
针对 xml 数据类型的 XQuery 运算符XQuery Operators Against the xml Data Type 介绍支持的 XQuery 运算符。Describes XQuery operators that are supported.
针对 xml 数据类型的其他示例 XQueryAdditional Sample XQueries Against the xml Data Type 提供附加的 XQuery 示例。Provides additional XQuery samples.

请参阅See Also

XML 数据 (SQL Server) XML Data (SQL Server)
XML 架构集合 (SQL Server) XML Schema Collections (SQL Server)
批量导入和导出 XML 文档的示例 (SQL Server)Examples of Bulk Import and Export of XML Documents (SQL Server)