External Variables

Prepared queries in XQJ are analogous to SQL prepared statements; they use XQuery external variables in the same way that SQL uses parameter markers. The DataDirect XQuery® SQL Adapter converts queries with external variables into SQL prepared statements (except when an external variable contains a sequence — this case is discussed in Example 11).

Example 10. XQuery External Variables

XQuery

declare variable $shares as xs:integer external;
for $h in collection('HOLDINGS')/HOLDINGS
where $h/SHARES < $shares
return $h/USERID

Generated SQL

SELECT ALL
nrm4."USERID" AS RACOL1
FROM
"PEPPINO"."HOLDINGS" nrm4
WHERE
nrm4."SHARES" < ?

Example 11. Sequences as External Variables

XQuery supports sequences as external variables, but in SQL, a parameter is a single value. DataDirect XQuery® uses temporary tables in the database to represent XQuery external variables that contain sequences. The sequence is inserted into the temporary table using batch insert in order to minimize the number of database round trips.

XQuery

declare variable $shares as xs:int* external;
for $h in collection('HOLDINGS')/HOLDINGS
where $h/SHARES = $shares
return $h/USERID

Generated SQL

INSERT INTO RATEMP009368710001( RAVAR, RAINT) VALUES(0, ?);

SELECT ALL
nrm4."USERID" AS RACOL2
FROM
"PEPPINO"."HOLDINGS" nrm4
WHERE
EXISTS(SELECT ALL
1 AS RACOL1
FROM
RATEMP009368710001 nrm7
WHERE
nrm7.RAVAR = 0 AND nrm4."SHARES" = nrm7.RAINT)
;

DELETE FROM RATEMP009368710001 WHERE RAVAR IN (0)

Try DataDirect XQuery® Free!

Put the power, scalability, and performance of DataDirect XQuery® to work for you today! Our free trial lets you see for yourself how easy it is to build data integration applications that access relational, EDI, and other file formats as XML!

Download DataDirect XQuery® today!

Online Video Tutorials!

Our easy-to-follow online video tutorials are a great way to get acquainted with the many features of DataDirect XQuery®.

And if you like what you see, download a free copy today and try DataDirect XQuery® for yourself!

Back to School with DataDirect XQuery®

Getting your mind around the possibilities of a data integration technology as promising as XQuery can be difficult, but our XML developers Webinars will help you understand the power and versatility of XQuery, and our favorite XQuery processor, DataDirect XQuery®.

New Features in DataDirect XQuery®

DataDirect XQuery® is now released!

DataDirect XQuery® provides full update support for relational data, easy integration for Web Services, additional enhancements for performance and scalability and more!

Download now!