SQL/MX 3.2.1 Programming Manual for C and COBOL (H06.26+, J06.15+)

Static Rowsets
HP NonStop SQL/MX Release 3.2.1 Programming Manual for C and COBOL663854-005
7-8
Selecting Rows Into Rowset Arrays
Example
This example uses a SELECT statement returning an employee’s first name, last
name, and department from the EMPLOYEE table. The elements in the target host
variable arrays are in the order based on the columns in the ORDER BY clause.
EXEC SQL BEGIN DECLARE SECTION;
char SQLSTATE[6];
ROWSET [100] unsigned NUMERIC (4) hva_deptnum;
ROWSET [100] char hva_firstname[16];
ROWSET [100] char hva_lastname[21];
...
long numrows;
EXEC SQL END DECLARE SECTION;
long i;
...
EXEC SQL
SELECT first_name, last_name, deptnum
INTO :hva_firstname, :hva_lastname, :hva_deptnum
FROM persnl.employee
ORDER BY deptnum, last_name, first_name;
...
EXEC SQL GET DIAGNOSTICS :numrows = ROW_COUNT;
...
for (i = 0; i < numrows; i++) {
/* NOTE: The null termination can also be done */
/* before the SELECT statement. */
hva_firstname[i][15] = '\0';
hva_lastname[i][20] = '\0';
printf("\nDept: %hu, Name: %s, %s",
hva_deptnum[i], hva_lastname[i], hva_firstname[i]);
...
/* Process the row in some way. */
....
}
...
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 sqlstate pic x(5).
01 numrows pic 9(9) comp.
01 rs.
02 ROWSET[100] hvadeptnum pic 9(4) comp.
02 ROWSET[100] hvafirstname pic x(15).
02 ROWSET[100] hvalastname pic x(20).
EXEC SQL END DECLARE SECTION END-EXEC.
01 i pic s9(4) comp.
...
EXEC SQL
SELECT first_name, last_name, deptnum
INTO :hvafirstname, :hvalastname, :hvadeptnum
FROM employee
ORDER BY deptnum, last_name, first_name END-EXEC.
EXEC SQL
C
COBOL