SQL/MP Programming Manual for C

Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for C429847-008
10-33
Allocate and Fill In Output Variables
The program can now prompt the user for the input parameter values, set the pointer
to the first SQLVAR element in the input SQLDA, and read through the SQLVAR array,
storing each value the user enters into the appropriate position in memory.
Allocate and Fill In Output Variables
To allocate space for output variables, you essentially perform the same set of
operations described for input parameters except that the pointers point to the output
SQLDA and names buffer. To get the descriptions of output variables into the output
SQLDA, use the DESCRIBE statement instead of DESCRIBE INPUT:
EXEC SQL DESCRIBE :statement_name
INTO :*output_sqlda_ptr
NAMES INTO :*output_namesbuf_ptr;
DESCRIBE places the descriptions of the variables to be output from the database into
the location in memory pointed to by output_sqlda_ptr, and the names of the
columns into the location pointed to by output_namesbuf_ptr. For code to allocate
memory, see Allocate Memory for the Values on page 10-31.
Handle Scale
If your program must handle numeric values with scale, read the scale information from
the output SQLDA structure. The DESCRIBE statement places this information in bits
0 through 7 of the data_len field in the sqlvar array. If you ignore scale, set the
data_len field to 0, causing data truncation. Otherwise, save the scale information
and write a function to handle the scale.
Display the Output
To display output from the database after the cursor FETCH, perform these steps:
1. Set pointers to the beginning of the first SQLVAR array and to the beginning of the
names buffer.
2. Get the number of output columns from the SQLDA structure.
3. Write the column name to the output file using the names buffer pointer (only if you
are doing a repetitive display of the column names).
4. Read the data_type field from the SQLVAR array to get the data type of the
column value to be written.
5. Write the value at the location pointed to by the var_ptr field from the SQLVAR
array. The steps to use depend on the data type of the value.
The sequence just described displays names and values repetitively. For example:
EMPNUM 2000
EMPNAME JANE ROBERTS
EMPNUM 1566