Use array statements to read or change more than one data record in a data table with only a single embedded SQL statement. An array statement executes an SQL statement multiple times in a loop, and applies each array element as a parameter in a row of the database table.
You have the following options when you formulate an array statement:
· You specify all parameters in a static SQL statement in array host variables.
· You use the FOR clause together with a descriptor and array program variables in a dynamic SQL statement.
EXEC SQL [<session_spec>] [FOR <loop_parameter>] <sql_statement>;
· Specify <session_spec>, when you are working with multiple database sessions.
· The value of <loop_parameter> determines the number of loop repeats. If the array variable has more or fewer components than specified in <loop_parameter>, a warning appears in the precompiler log and the statement is executed with the lowest dimension. This also applies if you use multiple array variables that do not have the same number of elements.
If errors occur in the database kernel while the array statement is being executed, the statement is terminated, and the number of successfully processed records is entered under sqlerrd[2] in the structure sqlca.
The SQL statement SELECT INTO is not permitted for array statements in the SQL mode INTERNAL.
Example of a static array statement:
/* Array Insert */
EXEC SQL BEGIN DECLARE SECTION;
int hno[3];
int zip[3];
float price[3];
char *name[3], *loc[3];
EXEC SQL END DECLARE SECTION;
hno[0] = 10;
name[0] = "Excelsior";
zip[0] = 89073;
loc[0] = "Philadelphia";
price[0] = 135.00;
hno[1] = 30;
name[1] = "Flora";
zip[1] = 48159;
loc[1] = "Atlanta";
price[1] = 45.00;
hno[2] = 20;
name[2] = "Continental";
zip[2] = 86165;
loc[2] = "Cincinnati";
price[2] = 70.00;
EXEC SQL INSERT INTO HOTEL VALUES (:hno, :name, :zip, :loc, :price);