Retrieving data

To retrieve rows from a database, you execute a select statement using SQLExecute or SQLExecDirect. This opens a cursor on the statement. Then, use SQLFetch or SQLFetchScroll with SQL_FETCH_NEXT option to fetch rows through the cursor. When an application frees the statement using SQLFreeStmt with SQL_CLOSE option, it closes the cursor.

To fetch values from a cursor, your application can use either SQLBindCol or SQLGetData:

SQLGetData is used to fetch values in pieces for columns such as LONG VARCHAR or LONG BINARY. As an alternative, you can set the SQL_ATTR_MAX_LENGTH statement attribute to a value large enough to hold the entire value for the column. For SQL_ATTR_MAX_LENGTH, the default value is 32KB.

The following code fragment from the simple sample opens a cursor on a query and retrieves data through the cursor. Error checking has been omitted to make the example easier to read.

SQLExecDirect( stmt, "select au_fname from authors ", SQL_NTS ) ;
retcode = SQLBindCol( stmt, 1, SQL_C_CHAR, aufName,
                      sizeof(aufName), &aufNameLen);
while(retcode == SQL_SUCCESS 
       || retcode == SQL_SUCCESS_WITH_INFO){
   retcode = SQLFetch( stmt );
}