Operator results and combining with other operatorsThe
result of the XMLEXISTS operator is a SQL boolean value that is based on the
results from evaluating the xqueryStringLiteral against
the xmlValueExpression. The XMLEXISTS operator returns:
- UNKNOWN
- When the xmlValueExpression is null.
- TRUE
- When the evaluation of the specified query expression against the specified
xmlValueExpression returns a non-empty sequence of nodes or values.
- FALSE
- When evaluation of the specified query expression against the specified
xmlValueExpression returns an empty sequence.
The XMLEXISTS operator does not return the actual results
from the evaluation of the query. You must use the XMLQUERY operator to retrieve
the actual results.
Since the result of the XMLEXISTS operator is an
SQL boolean data type, you can use the XMLEXISTS operator wherever a boolean
function is allowed. For example, you can use the XMLEXISTS operator as a
check constraint in a table declaration or as a predicate in a WHERE clause.
ExamplesIn the x_table table,
to determine if the xcol XML column for each row has an element
called student with an age attribute equal
to 20, use this statement:
SELECT id, XMLEXISTS('//student[@age=20]' PASSING BY REF xcol)
FROM x_table
In the x_table table,
to return the ID for every row whose xcol XML column is non-null
and contains the element /roster/student, use this statement:
SELECT id FROM x_table WHERE XMLEXISTS('/roster/student' PASSING BY REF xcol)
You
can create the x_table table with a check constraint that
limits which XML values can be inserted into the xcol XML
column. In this example, the constraint is that the column has at least one student element
with an age attribute with a value that is less than 25.
To create the table, use this statement:
CREATE TABLE x_table ( id INT, xcol XML
CHECK (XMLEXISTS ('//student[@age < 25]' PASSING BY REF xcol)) )