An array is a collection of data items associated with a single variable name. You can define an array of host variables (called host arrays) and operate on them with a single SQL statement.
You can use host arrays as input variables in INSERT, UPDATE and DELETE statements and as output variables in the INTO clause of SELECT and FETCH statements. This means that you can use arrays with SELECT, FETCH, DELETE, INSERT and UPDATE statements to manipulate large volumes of data.
As with simple host variables, you must declare host arrays in your program and then reference them in your SQL statements.
Host arrays are declared in much the same way as simple host variables using BEGIN DECLARE SECTION and END DECLARE SECTION. With host arrays, however, you must use the OCCURS clause to dimension the array.
The following example shows typical host array declarations and references.
EXEC SQL
BEGIN DECLARE SECTION
END-EXEC
01 AUTH-REC-TABLES
05 Auth-id OCCURS 25 TIMES PIC X(12).
05 Auth-Lname OCCURS 25 TIMES PIC X(40).
EXEC SQL
END DECLARE SECTION
END-EXEC.
. . .
EXEC SQL
CONNECT USERID 'user' IDENTIFIED BY 'pwd'
USING 'db_alias'
END-EXEC
EXEC SQL
SELECT au-id, au-lname
INTO :Auth-id, :Auth-Lname FROM authors
END-EXEC
display sqlerrd(3)
The following example demonstrates the use of the FOR clause, showing 10 rows (the value of :maxitems) modified by the UPDATE statement:
EXEC SQL
BEGIN DECLARE SECTION
END-EXEC
01 AUTH-REC-TABLES
05 Auth-id OCCURS 25 TIMES PIC X(12).
05 Auth-Lname OCCURS 25 TIMES PIC X(40).
01 maxitems PIC S9(4) COMP-5 VALUE 10.
EXEC SQL
END DECLARE SECTION
END-EXEC.
. . .
EXEC SQL
CONNECT USERID 'user' IDENTIFIED BY 'pwd'
USING 'db_alias'
END-EXEC
EXEC SQL
FOR :maxitems
UPDATE authors
SET au_lname = :Auth_Lname
WHERE au_id = :Auth_id
END-EXEC
display sqlerrd(3)