In XQuery, the result of an expression is a sequence that is made up of a list of XML nodes and instances of XSD atomic types. An individual entry in a sequence is referred to as an item. An item in a sequence can be either of the following:
-
A node such as an element, attribute, text, processing instruction, comment, or document
-
An atomic value such as an instance of an XSD simple type
For example, the following query constructs a sequence of two element-node items:
SELECT Instructions.query('
<step1> Step 1 description goes here</step1>,
<step2> Step 2 description goes here </step2>
') AS Result
FROM Production.ProductModel
WHERE ProductModelID=7;
This is the result:
<step1> Step 1 description goes here </step1>
<step2> Step 2 description goes here </step2>
In the previous query, the comma (,) at the end of the <step1> construction is the sequence constructor and is required. The white spaces in the results are added for illustration only and are included in all the example results in this documentation.
Following is additional information that you should know about sequences:
-
If a query results in a sequence that contains another sequence, the contained sequence is flattened into the container sequence. For example, the sequence ((1,2, (3,4,5)),6) is flattened in the data model as (1, 2, 3, 4, 5, 6).
DECLARE @x xml;
SET @x = '';
SELECT @x.query('(1,2, (3,4,5)),6');
-
An empty sequence is a sequence that does not contain any item. It is represented as "()".
-
A sequence with only one item can be treated as an atomic value, and vice versa. That is, (1) = 1.
In this implementation, the sequence must be homogeneous. That is, either you have a sequence of atomic values or a sequence of nodes. For example, the following are valid sequences:
DECLARE @x xml;
SET @x = '';
-- Expression returns a sequence of 1 text node (singleton).
SELECT @x.query('1');
-- Expression returns a sequence of 2 text nodes
SELECT @x.query('"abc", "xyz"');
-- Expression returns a sequence of one atomic value. data() returns
-- typed value of the node.
SELECT @x.query('data(1)');
-- Expression returns a sequence of one element node.
-- In the expression XML construction is used to construct an element.
SELECT @x.query('<x> {1+2} </x>');
The following query returns an error, because heterogeneous sequences are not supported.
SELECT @x.query('<x>11</x>, 22');