SQL/MX Programming Manual for Java
SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java—523726-003
3-52
Named Iterator
SQLJ NamedIterator interface and inherits its method signatures. See
sqlj.runtime.NamedIterator Interface on page B-5.
Declaring an Iterator Variable
After coding the iterator declaration clause, declare an iterator variable by using the
iterator class definition. Place this declaration among other Java variable declarations
within the class of the SQLJ source file. For example, declare the iterator iter by
using the NamIter class definition:
NamIter iter;
Assigning Query Results to a Named Iterator Object
Instantiate an iterator object by assigning the result set of a query to an iterator
variable. This step is similar to opening a cursor in an embedded SQL program in C or
COBOL. For example, use an assignment clause to bind the result set of a SELECT
statement to the iterator iter:
#sql iter = { SELECT salary, last_name
FROM samdbcat.persnl.employee };
For the syntax, see the Assignment Clause on page A-5.
Like positioned iterators, the query can be as complex as you desire and can include
GROUP BY, HAVING, ORDER BY, UNION, DISTINCT, and so on. However, the names
and SQL data types of the columns in the select list must correspond to the column
names and Java data types listed in the iterator declaration clause. For more
information, see Optimal Data Type Mappings on page 3-37.
The order and case of the column names in the select list do not matter, as the
previous example shows. The columns of the iterator declaration do not need to be in
the same order as the columns of the result set. The number of columns named in the
iterator must be the same as the number of columns retrieved in the result set, unless
you repeat the same column in the result set.
Using AS Clauses
If a SELECT statement that uses a named iterator selects columns with names that are
not valid Java identifiers, use AS clauses in the select list to provide acceptable column
names. For example, the column names cannot be Java reserved words, such as
package:
#sql iter = { SELECT package AS pkg, desc
FROM cat.sch.tab };
For more information about the AS clause, see the SELECT statement in the SQL/MX
Reference Manual.