Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time
title description author ms.author ms.date ms.service ms.subservice ms.topic helpviewer_keywords dev_langs
Primary Expressions (XQuery)
Learn about XQuery primary expressions that include literals, variable references, context item expressions, constructors, and function calls.
variable references [XQuery]
primary expressions [XQuery]
function calls [XQuery]
expressions [XQuery], primary
literals [XQuery]
context item expressions [XQuery]

Primary Expressions (XQuery)

[!INCLUDE SQL Server Azure SQL Database ]

The XQuery primary expressions include literals, variable references, context item expressions, constructors, and function calls.


XQuery literals can be numeric or string literals. A string literal can include predefined entity references, and an entity reference is a sequence of characters. The sequence starts with an ampersand that represents a single character that otherwise might have syntactic significance. Following are the predefined entity references for XQuery.

Entity reference Represents
&lt; <
&gt; >
&amp; &
&quot; "
&apos; '

A string literal can also contain a character reference, an XML-style reference to a Unicode character, that is identified by its decimal or hexadecimal code point. For example, the Euro symbol can be represented by the character reference, "&#8364;".

[!INCLUDEssNoVersion] uses XML version 1.0 as the basis for parsing.


The following examples illustrate the use of literals and also entity and character references.

This code returns an error, because the <' and '> characters have special meaning.

SET @var = ''  
SELECT @var.query(' <SalaryRange>Salary > 50000 and < 100000</SalaryRange>')  

If you use an entity reference instead, the query works.

SET @var = ''  
SELECT @var.query(' <SalaryRange>Salary &gt; 50000 and &lt; 100000</SalaryRange>')  

The following example illustrates the use of a character reference to represent the Euro symbol.

SET @var = ''  
SELECT @var.query(' <a>€12.50</a>')  

This is the result.


In the following example, the query is delimited by apostrophes. Therefore, the apostrophe in the string value is represented by two adjacent apostrophes.

SET @var = ''  
SELECT @var.query('<a>I don''t know</a>')  

This is the result.

<a>I don't know</a>

The built-in Boolean functions, true() and false(), can be used to represent Boolean values, as shown in the following example.

SET @var = ''  
SELECT @var.query('<a>{true()}</a>')  

The direct element constructor specifies an expression in curly braces. This is replaced by its value in the resulting XML.

This is the result.


Variable References

A variable reference in XQuery is a QName preceded by a $ sign. This implementation supports only unprefixed variable references. For example, the following query defines the variable $i in the FLWOR expression.

SET @var = '<root>1</root>'  
SELECT @var.query('  
 for $i in /root return data($i)')  

The following query will not work because a namespace prefix is added to the variable name.

SET @var = '<root>1</root>'  
SELECT @var.query('  
DECLARE namespace x="https://X";  
for $x:i in /root return data($x:i)')  

You can use the sql:variable() extension function to refer to SQL variables, as shown in the following query.

DECLARE @price money  
SET @price=2500  
DECLARE @x xml  
SET @x = ''  
SELECT @x.query('<value>{sql:variable("@price") }</value>')  

This is the result.


Implementation Limitations

These are the implementation limitations:

  • Variables with namespace prefixes are not supported.

  • Module import is not supported.

  • External variable declarations are not supported. A solution to this is to use the sql:variable() function.

Context Item Expressions

The context item is the item currently being processed in the context of a path expression. It is initialized in a not-NULL XML data type instance with the document node. It can also be changed by the nodes() method, in the context of XPath expressions or the [] predicates.

The context item is returned by an expression that contains a dot (.). For example, the following query evaluates each element <a> for the presence of attribute attr. If the attribute is present, the element is returned. Note that the condition in the predicate specifies that the context node is specified by single period.

SET @var = '<ROOT>  
<a attr="1">2</a>  
SELECT @var.query('/ROOT[1]/a[./@attr]')  

This is the result.

<a attr="1">2</a>

Function Calls

You can call built-in XQuery functions and the [!INCLUDEssNoVersion] sql:variable() and sql:column() functions. For a list of implemented functions, see XQuery Functions against the xml Data Type.

Implementation Limitations

These are the implementation limitations:

  • Function declaration in the XQuery prolog is not supported.

  • Function import is not supported.

See Also

XML Construction (XQuery)