XMLEXISTS predicate

The XMLEXISTS predicate tests whether an XQuery expression returns a sequence of one or more items.

Read syntax diagramSkip visual syntax diagramXMLEXISTS(xquery-expression-constantPASSINGBY REF,xquery-argument1)
Notes:
  • 1 xquery-context-item-expression must not be specified more than one time.

xquery-argument

Read syntax diagramSkip visual syntax diagramxquery-context-item-expressionxquery-context-item-expressionASidentifier
xquery-expression-constant
Specifies a character string constant that is interpreted as an XQuery expression using supported XQuery language syntax. For information about the XQuery language syntax, see Overview of pureXML. xquery-expression-constant cannot be an XQuery updating expression. The XQuery expression is evaluated with the arguments specified in xquery-argument. xquery-expression-constant must not be an empty string or a string of all blanks.
PASSING
Specifies input values and the manner in which these values are passed to the XQuery expression specified by xquery-expression-constant.
BY REF
Specifies that the XML input value arguments are to be passed by reference. When XML values are passed by reference, the XQuery evaluation uses the input node trees, preserving all properties including the original node identities and document order. If two arguments pass the same XML value, node identity comparisons and document ordering comparisons that involve some nodes that are contained between the two input arguments might refer to nodes within the same XML node tree.

This clause has no impact on how non-XML values are passed. The non-XML values create a new copy of the value during the cast to XML.

xquery-argument
Specifies an argument to use in the evaluation of the XQuery expression specified by xquery-expression-constant. A query argument is an expression that returns a value that is XML, integer, decimal, or a character or graphic string that is not a LOB. xquery-argument must not return ROWID, TIMESTAMP, binary string, REAL, DECFLOAT data types, or a character string data type that is bit data, and must not reference a sequence expression or a OLAP-specification.

An argument specifies a value and the manner in which that value is to be passed. How an argument in the PASSING clause is used in the XQuery expression depends on whether the argument is specified as the xquery-context-item-expression or an xquery-variable-expression. The argument includes an SQL expression that is evaluated before passing the result to the XQuery expression.

  • If the resulting value is an XML value, it becomes an input-xml-value. It is passed by reference which means that the original values, not copies, are used in the evaluation of the XQuery expression.
  • If the resulting value is not an XML value, the result of the expression must be able to be cast to an XML value. The cast value becomes an input-xml-value. An empty string is converted to an XML empty string.
  • If the resulting value is a null value, it is converted to an XML empty sequence if the argument is xquery-variable-expression. If the argument is xquery-context-expression, the XMLEXISTS predicates returns unknown.
xquery-context-item-expression
xquery-context-item-expression specifies the initial context item in the XQuery expression specified by xquery-expression-constant. The value of the initial context item is the result of xquery-context-item-expression cast to XML. xquery-context-item-expression must not be specified more than one time.

xquery-context-item-expression must not be a sequence of more than one item. If the result of xquery-context-item-expression is an empty string, the XQuery expression is evaluated with the initial context item set to an XML empty string.

If the xquery-context-item-expression is not specified or is an empty string, the initial context item in the XQuery expression is undefined, and the XQuery expression must not reference the initial context item. An XQuery variable is not created for the context item expression.

If the xquery-context-expression is not specified or the input-xml-value that results from the xquery-context-expression is an XML empty sequence, the initial context item is undefined. If the XQuery expression refers to the initial context item, it must be specified with a value that is not an XML empty sequence.

xquery-variable-expression
xquery-variable-expression specifies an argument to the XQuery expression. An XQuery variable is created for each xquery-variable-expression, and the XQuery variable is set to the result of xquery-argument-expression cast to XML. If the result of xquery-variable-expression is an empty string, the XQuery variable is set to an XML empty string. If xquery-variable-expression is null, the XQuery variable is set to an XML empty sequence. For example, PASSING T.A + T.B as "sum" creates an XQuery variable named sum. The scope of the XQuery variables created from the PASSING clause is the XQuery expression that is specified by xquery-expression-constant.
AS identifier
Specifies that the value that is generated by xquery-variable-expression will be passed to xquery-expression-constant as an XQuery variable named identifier. The length of the name must not be longer than 128 bytes. The leading dollar sign ($) that precedes variable names in the XQuery language is not included in identifier. The name must be an XML NCName that is not the same as identifier for another xquery-variable-expression in the same PASSING clause.

The result of the predicate is determined as follows:

  • The result is unknown if xquery-context-item-expression specified in the PASSING clause is a NULL value
  • the result is false if the result of the XQuery expression is an empty sequence
  • the result is true in all other cases

If the evaluation of the XQuery expression results in an error, XMLEXISTS returns an error. The XMLEXISTS predicate is not supported in ON clause of outer joins.

Example: Find all the purchase orders that buy a baby monitor. This example finds the product number for baby monitors from the product table and joins the result to the PurchaseOrders table. It then evaluates the XQuery expression //item[@partnum = $n] for each row and returns those rows that contain an item element node with a partNum attribute that is equal to the product number of ‘Baby Monitor'. The context item for the XQuery expression is PO.POrder. An XQuery variable, $n, is created and initialized to the value of S.prodno:
   SELECT S.prodno, count(*) as result
      FROM PurchaseOrders PO, Products S
      WHERE XMLEXISTS ('//item[@partNum = $n]'
                       PASSING PO.POrder,
                               S.prodno AS "n")
      AND S.prod_name = 'Baby Monitor';
The results might be similar to the following:
Prodno      result
-------------------------------

926-AA      1