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-6
Using Argument Lists in Dynamic SQL
/* Move statement with input variable to statement variable. */
strcpy(hv_sql_stmt, "SELECT empnum, first_name,"
" last_name, salary"
" FROM samdbcat.persnl.employee"
" WHERE empnum = CAST(? AS NUMERIC(4) UNSIGNED)");
/* Prepare the statement. */
EXEC SQL PREPARE sqlstmt FROM :hv_sql_stmt;
/* Initialize the input parameter in the WHERE clause. */
printf("Enter the employee number to be retrieved: ");
scanf("%hu", &in_empnum);
/* Execute the prepared statement using the argument lists. */
EXEC SQL EXECUTE sqlstmt
USING :in_empnum
INTO :hv_empnum, :hv_first_name, :hv_last_name,
:hv_salary INDICATOR :hv_salary_i;
if (strcmp(SQLSTATE, SQLSTATE_OK) == 0) {
/* Process the output values. */
printf("\nEmpnum is: %hu", hv_empnum);
hv_first_name[15]='\0';
printf("\nFirst name is: %s", hv_first_name);
hv_last_name[20]='\0';
printf("\nLast name is: %s", hv_last_name);
if (hv_salary_i < 0)
printf("\nSalary is unknown\n\n");
else
printf("\nSalary is: %.2f\n\n", hv_salary/100.0);
} else if (strcmp(SQLSTATE, SQLSTATE_NODATA) == 0)
printf("\nNo row with employee number %hu\n\n", in_empnum);
/* Deallocate the prepared statement. */
EXEC SQL DEALLOCATE PREPARE sqlstmt;
Example A-3. Using Argument Lists in Dynamic SQL (page 2 of 3)