SQL/MP Programming Manual for C
Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for C—429847-008
10-25
Read and Compile the SQL Statement
a. Check the data_type field and, if necessary, adjust the data type so that the
C program can handle and reset data_len accordingly.
b. Allocate an amount of memory equal to the data_len field for the parameter.
c. Set the var_ptr field to point to the memory.
If you are not allocating memory dynamically, declare a variable for each input
parameter value, and put the address of the variable in var_ptr.
If you know the number and data type of your input parameter values, you set
only data_type, data_len, and var_ptr.
Some programs might check data_type and data_len when the actual
values are obtained.
d. If you are handling null values, check the null_info field and continue
according to its value:
If necessary, set ind_ptr to point to the memory allocated in Step c. (If you
are not allocating memory dynamically, define a variable for the indicator and
put its address in ind_ptr.)
6. Loop through the names buffer to read the corresponding name for each
parameter and prompt the user for each value. Read each value into the
corresponding occurrence in the input data buffer, according to the data type of the
value. If the parameter can be null (null_info is -1) and the value entered was
null, set ind_ptr to -1.
Read and Compile the SQL Statement
1. Read the SQL statement you want to run. Normally, a user enters this statement
from a terminal or workstation. After you construct the statement, pad the
remainder of the buffer, including the null terminator position, with blanks.
2. Assign a statement name to the host variable (if necessary).
char statement_name[11];
strncpy ( statement_name, "stmt1", 2 );
3. Compile the SQL statement using the PREPARE statement:
EXEC SQL PREPARE :statement_name FROM :statement_buffer;
Process the Output Variables
If the output_num field in the SQLSA structure is greater than zero (0) after the
PREPARE statement runs, perform these steps:
0 Do not allocate any memory.
-1 Allocate 2 bytes of memory for the indicator value.