Constant and parameter marker passing to XMLEXISTS and XMLQUERY
You can specify XQuery variables as part of the XQuery expression in the XMLEXISTS predicate and the XMLQUERY function.
You pass the values into these variables through the passing
clause.
These values are SQL expressions.
Because the values that are passed to the XQuery expression are non-XML values, they must be cast, either implicitly or explicitly, to types that are supported by XQuery.
Example: Implicit casting
In
the following query, the SQL character string constant 'Aurora', which
is not an XML type, is implicitly cast to an XML type in the XMLEXISTS
predicate. Following the implicit cast, the constant has the XML schema
subtype of xs:string, and is bound to the variable $cityName. The
$cityName variable can then be used in the predicate of the XQuery expression.
SELECT XMLQUERY ('declare default element namespace "http://posample.org";
$d/customerinfo/addr' passing c.INFO as "d")
FROM Customer as c
WHERE XMLEXISTS('declare default element namespace "http://posample.org";
$d//addr[city=$cityName]'
passing c.INFO as "d",
'Aurora' AS "cityName")
Example: Explicit casting
In
the following query, the XMLCAST specification casts a string constant
to the XML data type.
SELECT XMLQUERY ('declare default element namespace "http://posample.org";
$d/customerinfo/addr' passing c.INFO as "d")
FROM Customer as c
WHERE XMLEXISTS('declare default element namespace "http://posample.org";
$d//addr[city=$cityName]'
passing c.INFO as "d",
XMLCAST('San Jose' as XML) AS "cityName")