Quantified expressions
Quantified expressions return true if some or every item in one or more sequences satisfies a specific condition. The value of a quantified expression is always true or false.
A quantified expression begins with a quantifier (some or every) that indicates whether the expression performs existential or universal quantification. The quantifier is followed by one or more clauses that bind variables to items that are returned by expressions. The bound variables are then referenced in a test expression to determine if some or all of the bound values satisfy a specific condition.
Syntax
- some
- When this keyword is specified, the quantified expression returns true if the effective boolean value of TestExpression is true for at least one item that is returned by Expression. Otherwise, the quantified expression returns false.
- every
- When this keyword is specified, the quantified expression returns true if the effective boolean value of TestExpression is true for every item that is returned by Expression. Otherwise, the quantified expression returns false.
- VariableName
- The name of the variable to bind to each item in the result of Expression. Variables that are bound in a quantified expression are in scope for all of the sub-expressions that appear after the variable binding in the quantified expression.
- Expression
- Any XQuery expression. If the expression includes a top-level comma operator, then the expression must be enclosed in parentheses.
- satisfies
- The keyword that directly precedes the test expression
- TestExpression
- An XQuery expression that specifies the condition that must be met by some or every item in the sequences returned by Expression.
Note: When errors occur, the result of a quantified comparison
can be either a boolean value or an error.
Examples
- The quantified expression in the following example returns true if every
customer in the CUSTOMER.INFO column of the SAMPLE database has an address
in Canada:
every $cust in db2-fn:xmlcolumn('CUSTOMER.INFO')/customerinfo satisfies $cust/addr/@country = "Canada"
- In the following examples, each quantified expression evaluates its test
expression for every combination of values that are bound to the variables
a
andb
(there are nine combinations in all).The result of the following expression is true:some $a in (3, 5, 9), $b in (1, 3, 5) satisfies $a * $b = 27
The result of the following expression is false:every $a in (3, 5, 9), $b in (1, 3, 5) satisfies $a * $b = 27
- The following example demonstrates that the result of a quantified expression
is not deterministic in the presence of errors. The expression can either
return true or an error because the test expression returns true for one variable
binding and returns an error for another:
Likewise, the following expression can return false or an error:some $a in (3, 5, "six") satisfies $a * 3 = 9
every $a in (3, 5, "six") satisfies $a * 3 = 9