Xml Query Tool

-->

Applies to: SQL Server (all supported versions)

Specifies an XQuery against an instance of the xml data type. The result is of xml type. The method returns an instance of untyped XML.

Syntax

Xml Query Tool Software

Note

There has been enormous activity in querying xml in the database community. W3C has come out with XQuery and is being implemented by major players. This tool is being developed as a front end to generate basic queries so that end users. XML is the most powerful data storage and transfer medium on the web. It works as XML Viewer, XML Formatter, XML Editor,XML Validator. What can you do with XML Viewer/ XML Formatter? It helps to beautify/format your XML. It helps to display your XML in a tree view. This also works as XML Pretty Print. It helps to minify your XML.

To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.

Arguments

XQuery
Is a string, an XQuery expression, that queries for XML nodes, such as elements and attributes, in an XML instance.

Examples

This section provides examples of using the query() method of the xml data type.

A. Using the query() method against an xml type variable

The following example declares a variable @myDoc of xml type and assigns an XML instance to it. The query() method is then used to specify an XQuery against the document.

Xml query tool

The query retrieves the <Features> child element of the <ProductDescription> element:

The following output shows the result:

B. Using the query() method against an XML type column

In the following example, the query() method is used to specify an XQuery against the CatalogDescription column of xml type in the AdventureWorks database:

Note the following items from the previous query:

  • The CatalogDescription column is a typed xml column, which means it has a schema collection associated with it. In the XQuery Prolog, the namespace keyword defines the prefix that's later used in the query body.

  • The query() method constructs XML, a <Product> element that has a ProductModelID attribute, in which the ProductModelID attribute value is retrieved from the database. For more information about XML construction, see XML Construction (XQuery).

  • The exist() method (XML data type) in the WHERE clause finds only rows that contain the <Warranty> element in the XML. Again, the namespace keyword defines two namespace prefixes.

The following output shows the partial result:

Note the query() and exist() methods both declare the PD prefix. In these cases, you can use WITH XMLNAMESPACES to first define the prefixes and use it in the query.

Xml Query Tool Download

Xml query tool code

See Also

Add Namespaces to Queries with WITH XMLNAMESPACES
Compare Typed XML to Untyped XML
Create Instances of XML Data
xml Data Type Methods
XML Data Modification Language (XML DML)