SQL/MP Programming Manual for C
Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for C—429847-008
10-19
Dynamic Allocation of Memory
provide names for the SQLDA and names buffer as shown:
EXEC SQL INCLUDE SQLDA (dummy_da, 1, dummy_namesbuf, 1);
The INCLUDE directive generates the structure templates sqlda_type and 
sqlvar_type, which you can later use to allocate the memory. You might set up 
the pointers that will eventually point to that memory. For example:
typedef struct SQLDA_TYPE *sqldaptr;
sqldaptr input_sqlda_ptr, output_sqlda_ptr;
When the memory is allocated, input_sqlda_ptr points to the memory for the 
input SQLDA, and output_sqlda_ptr points to the memory for the output 
SQLDA. To access the SQLDA and names buffer, declare pointers and then pass 
the pointers to a function that allocates the memory as follows: 
EXEC SQL BEGIN DECLARE SECTION;
 typedef struct SQLDA_TYPE *sqldaptr;
 sqldaptr input_sqlda_ptr, output_sqlda_ptr;
 typedef char (*arrayptr) [1000];
 arrayptr input_namesbuf_ptr, output_namesbuf_ptr;
EXEC SQL END DECLARE SECTION;
To give the DESCRIBE INPUT and DESCRIBE statements a size to use before the 
memory is actually allocated, you must declare the names buffer to be an 
arbitrarily large size (this example uses 1000). Estimate a number that is greater 
than any possible size your names buffer could be. Otherwise, DESCRIBE INPUT 
and DESCRIBE might stop describing parameters or variables too soon. 
3. Declare an SQLSA structure using the INCLUDE SQLSA directive:
EXEC SQL INCLUDE SQLSA;
4. Dynamically compile the SQL statement (stmt1) entered by the user using the 
PREPARE statement as shown in this example: 
#define MAX_QUERY_SIZE 512
EXEC SQL BEGIN DECLARE SECTION;
 char statement_buffer[MAX_QUERY_SIZE + 1];
...
EXEC SQL END DECLARE SECTION;
...
printf("\nEnter a new SQL statement:\n");
/* Pass statement_buffer to a function that reads */
/* and parses the input (code is in sample program) */
 ...
EXEC SQL PREPARE stmt1 FROM :statement_buffer;
5. Use the information in the SQLSA structure to determine the number of input 
parameters and output variables in the statement.
6. Allocate space for the required number of SQLDA data structures to describe the 
parameters and output variables using the malloc function.
7. Allocate space for the values input to the program or output from the database, 
again using the malloc function.










