SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)

C Sample Programs
HP NonStop SQL/MX Programming Manual for C and COBOL523627-004
A-9
Using SQL Descriptor Areas With DESCRIBE
SQLSTATE[5]='\0';
SQLSTATE_OK[5]='\0';
printf("\n\nThis example uses SQL descriptor areas. \n\n");
EXEC SQL WHENEVER SQLERROR GOTO end_prog;
/* Initialize the output variables in the SELECT list. */
printf("Enter columns to be retrieved, separate by commas: \n");
gets(in_columns);
/* Concatenate statement with input and output variables. */
strcpy(hv_sql_stmt, "SELECT ");
strcat(hv_sql_stmt, in_columns);
strcat(hv_sql_stmt, " FROM samdbcat.persnl.employee"
" WHERE empnum = CAST(? AS NUMERIC(4) UNSIGNED)");
/* Allocate the descriptor area for input parameters. */
hv_desc_max = 1;
EXEC SQL ALLOCATE DESCRIPTOR 'in_sqlda' WITH MAX :hv_desc_max;
/* Allocate the descriptor area for output values. */
hv_desc_max = 6;
EXEC SQL ALLOCATE DESCRIPTOR 'out_sqlda' WITH MAX :hv_desc_max;
/* Prepare the statement. */
EXEC SQL PREPARE sqlstmt FROM :hv_sql_stmt;
/* Describe the SQL descriptor area for input parameter. */
EXEC SQL DESCRIBE INPUT sqlstmt
USING SQL DESCRIPTOR 'in_sqlda';
/* Describe the SQL descriptor area for SELECT values. */
EXEC SQL DESCRIBE OUTPUT sqlstmt
USING SQL DESCRIPTOR 'out_sqlda';
/* Initialize the input parameter in the WHERE clause. */
printf("Enter the employee number to be retrieved: ");
scanf("%hu", &in_empnum);
/* Set the value of the input parameter in */
/* the input SQL descriptor area. */
hv_desc_value = 1;
EXEC SQL SET DESCRIPTOR 'in_sqlda' VALUE :hv_desc_value
VARIABLE_DATA = :in_empnum;
/* Execute the prepared statement using */
/* the SQL descriptor areas. */
EXEC SQL EXECUTE sqlstmt
USING SQL DESCRIPTOR 'in_sqlda'
INTO SQL DESCRIPTOR 'out_sqlda';
Example A-4. Using SQL Descriptor Areas With DESCRIBE (page 2 of 4)