Retrieving query results is part of the larger task of processing transactions in CLI applications. Retrieving query results involves binding application variables to columns of a result set and then fetching the rows of data into the application variables. A typical query is the SELECT statement.
To retrieve each row of the result set:
struct
{
SQLINTEGER ind;
SQLSMALLINT val;
}
deptnumb; /* variable to be bound to the DEPTNUMB column */
struct
{
SQLINTEGER ind;
SQLCHAR val[15];
}
location; /* variable to be bound to the LOCATION column */
/* ... */
/* bind column 1 to variable */
cliRC = SQLBindCol(hstmt, 1, SQL_C_SHORT, &deptnumb.val, 0,
&deptnumb.ind);
STMT_HANDLE_CHECK(hstmt, hdbc, cliRC);
/* bind column 2 to variable */
cliRC = SQLBindCol(hstmt, 2, SQL_C_CHAR, location.val, 15,
&location.ind);
STMT_HANDLE_CHECK(hstmt, hdbc, cliRC);
/* fetch each row and display */
cliRC = SQLFetch(hstmt);
if (cliRC == SQL_NO_DATA_FOUND)
{
printf("\n Data not found.\n");
}
while (cliRC != SQL_NO_DATA_FOUND)
{
printf(" %-8d %-14.14s \n", deptnumb.val, location.val);
/* fetch next row */
cliRC = SQLFetch(hstmt);
}
SQLFetchScroll() can also be used to fetch multiple rows of the result set into an array.
If data conversion was required for the data types specified on the call to SQLBindCol(), the conversion will occur when SQLFetch() is called.
/* fetch each row and display */
cliRC = SQLFetch(hstmt);
if (cliRC == SQL_NO_DATA_FOUND)
{
printf("\n Data not found.\n");
}
while (cliRC != SQL_NO_DATA_FOUND)
{
/* use SQLGetData() to get the results */
/* get data from column 1 */
cliRC = SQLGetData(hstmt,
1,
SQL_C_SHORT,
&deptnumb.val,
0,
&deptnumb.ind);
STMT_HANDLE_CHECK(hstmt, hdbc, cliRC);
/* get data from column 2 */
cliRC = SQLGetData(hstmt,
2,
SQL_C_CHAR,
location.val,
15,
&location.ind);
/* display the data */
printf(" %-8d %-14.14s \n", deptnumb.val, location.val);
/* fetch the next row */
cliRC = SQLFetch(hstmt);
}