Each application that will make use of scrollable cursors must complete the following steps, in the following order:
The following additional statement attributes are required when using scrollable cursors in DB2 CLI applications. See SQLSetStmtAttr - Set Options Related to a Statement for complete details.
The default value is 1.
This value must be set or the default value of SQL_CURSOR_FORWARD_ONLY will be used.
Set the SQL_ATTR_ROWS_FETCHED_PTR statement attribute as a pointer to a SQLUINTEGER variable. This variable will then contain the number of rows returned in the rowset after each call to SQLFetchScroll().
For more information see "Row Status Array".
The following example demonstrates the required calls to SQLSetStmtAttr():
/* CLI Sample: sfetch.c */ /* ... */ /* Set the number of rows in the rowset */ rc = SQLSetStmtAttr( hstmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) ROWSET_SIZE, 0); CHECK_STMT(hstmt, rc); /* Set the SQL_ATTR_ROWS_FETCHED_PTR statement attribute to */ /* point to the variable numrowsfetched: */ rc = SQLSetStmtAttr( hstmt, SQL_ATTR_ROWS_FETCHED_PTR, &numrowsfetched, 0); CHECK_STMT(hstmt, rc); /* Set a pointer to the array to use for the row status */ rc = SQLSetStmtAttr( hstmt, SQL_ATTR_ROW_STATUS_PTR, (SQLPOINTER) row_status, 0); CHECK_STMT(hstmt, rc); /* Set the cursor type */ rc = SQLSetStmtAttr( hstmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER) SQL_CURSOR_STATIC, 0); CHECK_STMT(hstmt, rc); /* Indicate that we will use bookmarks by setting the */ /* SQL_ATTR_USE_BOOKMARKS statement attribute to SQL_UB_VARIABLE: */ rc = SQLSetStmtAttr( hstmt, SQL_ATTR_USE_BOOKMARKS, (SQLPOINTER) SQL_UB_VARIABLE, 0); CHECK_STMT(hstmt, rc); /* ... */
Follow the usual DB2 CLI process for executing an SQL statement and binding the result set. The application can call SQLRowCount() to determine the number of rows in the overall result set. Scrollable cursors support the use of both column wise and row wise binding. The CLI sample program sfetch.c demonstrates the use of both methods.
At this point the application can read information from the result set using the following steps:
A typical call to SQLFetchScroll() to retrieve the first rowset of data would be as follows:
SQLFetchScroll(hstmt, SQL_FETCH_FIRST, 0);
If you have set the SQL_ATTR_ROW_STATUS_PTR statement attribute then the row status array will also be updated for each possible row in the rowset. For more information see "Row Status Array".
Once the application has finished retrieving information it should follow the usual DB2 CLI process for freeing a statement handle.