|
For an introduction on how to determine input parameters, see Determining Unknown Input Parameters. This section describes how to handle a parameterized SELECT statement with an sqlda structure. If a prepared SELECT statement has a WHERE clause with input parameters of unknown number and data type, your ESQL/C program must use an sqlda structure to define the input parameters.
Important: If the SELECT statement has unknown columns in the select list, your program must also handle these columns with an sqlda structure. For more information, see Handling an Unknown Select List.
The following sections demonstrate how to use an sqlda structure to handle a parameterized SELECT statement that returns many rows and one that returns only a single row.
The sample program described on the following pages is a modified version of the demo4.ec example program. It shows how to use a dynamic SELECT statement with the following conditions:
The program illustrates how to use an sqlda structure to handle both input parameters of a WHERE clause and the columns in the select list.
The program must include the ESQL/C sqlda.h header file to use an sqlda structure.
Lines 9 to 11 declare variables to hold the data that is obtained from the user. The sqlda_ptr variable (line 14) is the pointer to an sqlda structure. The pos variable (line 13) points to an sqlvar_struct structure so that the code can proceed through each of the sqlvar_struct structures in the variable-length portion of sqlda. Neither of these variables is defined as an ESQL/C host variable.
These lines assemble the character string for the SELECT statement and prepare the SELECT string. This program assumes the number and data types of the input parameters. Therefore, no C code needs to determine this information at runtime. The question mark (?) indicates the input parameters in the WHERE clause. For more information on these steps, see Assembling and Preparing the SQL Statement.
This line declares the slct_cursor cursor for the prepared statement identifier, sql_id.
These lines initialize the sqlda structure with the input parameter information. The program assumes two input parameters (line 22). If the number of input parameters is unknown, the program needs to parse the SELECT character string (not the prepared version) and count the number of "?" characters that it contains.
The program then calls the whereClauseMem() function to allocate and initialize the sqlda structure. For more information, see lines 69 to 77.
The database server executes the SELECT statement when it opens the cursor. You must include the USING DESCRIPTOR clause of OPEN to specify the sqlda structure as the location of the input parameter values.
Once the OPEN...USING DESCRIPTOR statement has executed, these input parameter values have been used. Deallocate this sqlda structure because it is no longer needed and so that it does not conflict with the sqlda that contains the retrieved values. Keep in mind that this second sqlda must have memory allocated before it can be used.
For demonstration purposes, this program assumes that the number and data types of the select-list columns are also unknown at compile time. It uses the DESCRIBE...INTO statement (line 27) to allocate an sqlda structure, and puts information about the select-list columns into the structure to which sqlda_ptr points.
The selectListMem() function handles the allocation of memory for column values. For more information on selectListMem(), see lines 85 to 102.
The while loop executes for each row fetched from the database. The FETCH statement (line 31) includes the USING DESCRIPTOR clause to specify an sqlda structure as the location for the returned column values. For more information on how to handle unknown select-list columns, see Handling an Unknown Select List.
These lines test the value of the SQLCODE variable to determine if the FETCH was successful. If SQLCODE contains a nonzero value, then the FETCH generates the NOT FOUND condition (100) or an error (< 0). In any of these cases, line 34 prints out the SQLCODE value. To determine if the FETCH statement generated warnings, you need to examine the sqlca.sqlwarn structure.
These lines access the fields of the sqlvar_struct structure for each column in the select list. The prsqlda() function (see lines 75 to 81) displays the column name (from sqlvar_struct.sqlname) and its value (from the sqlvar_struct.sqldata field). The switch (lines 41 to 55) transfers the column values from the sqlda structure into host variables of the appropriate lengths and data types.
These lines free resources after all the rows are fetched. Line 60 closes the slct_cursor cursor and line 61 frees it. Line 62 frees the sql_id statement ID.
These free() system calls release the memory that is associated with the sqlda structure. Line 63 releases the memory allocated to the sqlvar_struct structures. Line 64 releases the memory allocated for the sqlda structure. The program does not need to deallocate memory associated with the sqldata fields because these fields have used space that is in a data buffer. For more information, see Freeing Memory Allocated to an sqlda Structure.
The whereClauseMem() function initializes the sqlda structure with the input-parameter definitions. Line 74 allocates memory for an sqlda structure to hold the input parameters in the WHERE clause. Use of a DESCRIBE...INTO statement to allocate an sqlda results in an sqlda that holds information about the select-list columns of the SELECT. Because you want to describe the input parameters in the WHERE clause, do not use DESCRIBE here.
Line 75 sets the sqld field of the sqlda structure to the value of count (2) to indicate the number of parameters that are in the WHERE clause. Lines 76 and 77 use the calloc() system function to allocate the memory so that each input parameter in the WHERE clause has an sqlvar_struct structure. These lines then set the sqlvar field of the sqlda structure so that it points to this sqlvar_struct memory.
Lines 78 to 80 set the sqltype, sqllen, and sqldata fields of the sqlvar_struct structure to describe the first input parameter: a character (CCHARTYPE) host variable of length 16 (FNAME + 1) whose data is stored in the fname buffer. The fname buffer is a character buffer declared in the main() program and passed as an argument to whereClauseMem().
Lines 81 to 83 set the sqltype, sqllen, and sqldata fields of the sqlvar_struct structure to describe the second input parameter. This parameter is for the lname column. It is defined in the same way as the fname column (lines 78 to 80) but it receives its data from the lname buffer [also passed from main() to whereClauseMem()].
The selectListMem() function allocates the memory and initializes the sqlda structure for the unknown select-list columns of the parameterized SELECT statement. For more information on how to use an sqlda structure for select-list columns, see Handling a Parameterized SELECT Statement.
The instructions in the previous section assume that the parameterized SELECT statement returns more than one row and, therefore, is associated with a cursor. If you know at the time that you write the program that the parameterized SELECT statement always returns just one row, you can omit the cursor and use the EXECUTE...USING DESCRIPTOR...INTO statement instead of the OPEN...USING DESCRIPTOR statement to specify parameters values from an sqlda structure. For more information, see Specifying Input Parameter Values.