SQLFetch - Fetch next row
SQLFetch() advances the cursor to the next row of the result set, and retrieves any bound columns.
SQLFetch() can be used to receive the data directly into variables that you specify with SQLBindCol(), or the columns can be received individually after the fetch by calling SQLGetData(). Data conversion is also performed when SQLFetch() is called, if conversion is indicated when the column is bound.
Syntax
SQLRETURN SQLFetch (SQLHSTMT hstmt);
Function arguments
Table 1. SQLFetch arguments Data type argument Use Description SQLHSTMT hstmt Input Statement handle
Usage
SQLFetch() can only be called if the most recently processed statement on hstmt is a SELECT.
The number of application variables bound with SQLBindCol() must not exceed the number of columns in the result set; otherwise SQLFetch() fails.
If SQLBindCol() has not been called to bind any columns, then SQLFetch() does not return data to the application, but just advances the cursor. In this case SQLGetData() can then be called to obtain all of the columns individually. Data in unbound columns is discarded when SQLFetch() advances the cursor to the next row.
If any bound variables are not large enough to hold the data returned by SQLFetch(), the data is truncated. If character data is truncated, and the SQLSetEnvAttr() attribute SQL_ATTR_TRUNCATION_RTNC is set to SQL_TRUE, then the CLI return code SQL_SUCCESS_WITH_INFO is returned, along with an SQLSTATE that indicates truncation. Note that the default is SQL_FALSE for SQL_ATTR_TRUNCATION_RTNC. Also, in the case of character data truncation, the SQLBindCol() deferred output argument pcbValue contains the actual length of the column data retrieved from the data source. The application should compare the output length to the input length (pcbValue and cbValueMax arguments from SQLBindCol()) to determine which character columns have been truncated.
Truncation of numeric data types is not reported if the truncation involves digits to the right of the decimal point. If truncation occurs to the left of the decimal point, an error is returned (refer to the diagnostics section).
Truncation of graphic data types is treated the same as character data types. Except the rgbValue buffer is filled to the nearest multiple of two bytes that is still less than or equal to the cbValueMax specified in SQLBindCol(). Graphic data transferred between DB2® UDB CLI and the application is never null-terminated.
When all the rows have been retrieved from the result set, or the remaining rows are not needed, SQLFreeStmt() should be called to close the cursor and discard the remaining data and associated resources.
Return codes
- SQL_SUCCESS
- SQL_SUCCESS_WITH_INFO
- SQL_ERROR
- SQL_INVALID_HANDLE
- SQL_NO_DATA_FOUND
SQL_NO_DATA_FOUND is returned if there are no rows in the result set, or previous SQLFetch() calls have fetched all the rows from the result set.
Diagnostics
Table 2. SQLFetch SQLSTATEs SQLSTATE Description Explanation 01004 Data truncated The data returned for one or more columns is truncated. String values are right truncated. (SQL_SUCCESS_WITH_INFO is returned if no error occurred.) HY001 Memory allocation failure The driver is unable to allocate memory required to support the processing or completion of the function. HY010 Function sequence error The specified hstmt is not in an processed state. The function is called without first calling SQLExecute or SQLExecDirect. HY013 * Memory management problem The driver is unable to access memory required to support the processing or completion of the function.
Example
By using the code examples, you agree to the terms of the Code license and disclaimer information.
/************************************************************************* ** file = fetch.c ** ** Example of executing an SQL statement. ** SQLBindCol & SQLFetch is used to retrieve data from the result set ** directly into application storage. ** ** Functions used: ** ** SQLAllocConnect SQLFreeConnect ** SQLAllocEnv SQLFreeEnv ** SQLAllocStmt SQLFreeStmt ** SQLConnect SQLDisconnect ** ** SQLBindCol SQLFetch ** SQLTransact SQLExecDirect ** SQLError ** **************************************************************************/ #include <stdio.h> #include <string.h> #include "sqlcli.h" #define MAX_STMT_LEN 255 int initialize(SQLHENV *henv, SQLHDBC *hdbc); int terminate(SQLHENV henv, SQLHDBC hdbc); int print_error (SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt); int check_error (SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt, SQLRETURN frc); /******************************************************************* ** main ** - initialize ** - terminate *******************************************************************/ int main() { SQLHENV henv; SQLHDBC hdbc; SQLCHAR sqlstmt[MAX_STMT_LEN + 1]=""; SQLRETURN rc; rc = initialize(&henv, &hdbc); if (rc == SQL_ERROR) return(terminate(henv, hdbc)); {SQLHSTMT hstmt; SQLCHAR sqlstmt[]="SELECT deptname, location from org where division = 'Eastern'"; SQLCHAR deptname[15], location[14]; SQLINTEGER rlength; rc = SQLAllocStmt(hdbc, &hstmt); if (rc != SQL_SUCCESS ) check_error (henv, hdbc, SQL_NULL_HSTMT, rc); rc = SQLExecDirect(hstmt, sqlstmt, SQL_NTS); if (rc != SQL_SUCCESS ) check_error (henv, hdbc, hstmt, rc); rc = SQLBindCol(hstmt, 1, SQL_CHAR, (SQLPOINTER) deptname, 15, &rlength); if (rc != SQL_SUCCESS ) check_error (henv, hdbc, hstmt, rc); rc = SQLBindCol(hstmt, 2, SQL_CHAR, (SQLPOINTER) location, 14, &rlength); if (rc != SQL_SUCCESS ) check_error (henv, hdbc, hstmt, rc); printf("Departments in Eastern division:\n"); printf("DEPTNAME Location\n"); printf("-------------- -------------\n"); while ((rc = SQLFetch(hstmt)) == SQL_SUCCESS) { printf("%-14.14s %-13.13s \n", deptname, location); } if (rc != SQL_NO_DATA_FOUND ) check_error (henv, hdbc, hstmt, rc); rc = SQLFreeStmt(hstmt, SQL_DROP); if (rc != SQL_SUCCESS ) check_error (henv, hdbc, SQL_NULL_HSTMT, rc); } rc = SQLTransact(henv, hdbc, SQL_COMMIT); if (rc != SQL_SUCCESS ) check_error (henv, hdbc, SQL_NULL_HSTMT, rc); terminate(henv, hdbc); return (0); }/* end main */ /******************************************************************* ** initialize ** - allocate environment handle ** - allocate connection handle ** - prompt for server, user id, & password ** - connect to server *******************************************************************/ int initialize(SQLHENV *henv, SQLHDBC *hdbc) { SQLCHAR server[SQL_MAX_DSN_LENGTH], uid[30], pwd[30]; SQLRETURN rc; rc = SQLAllocEnv (henv); /* allocate an environment handle */ if (rc != SQL_SUCCESS ) check_error (*henv, *hdbc, SQL_NULL_HSTMT, rc); rc = SQLAllocConnect (*henv, hdbc); /* allocate a connection handle */ if (rc != SQL_SUCCESS ) check_error (*henv, *hdbc, SQL_NULL_HSTMT, rc); printf("Enter Server Name:\n"); gets(server); printf("Enter User Name:\n"); gets(uid); printf("Enter Password Name:\n"); gets(pwd); if (uid[0] == '\0') { rc = SQLConnect (*hdbc, server, SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS); if (rc != SQL_SUCCESS ) check_error (*henv, *hdbc, SQL_NULL_HSTMT, rc); } else { rc = SQLConnect (*hdbc, server, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS); if (rc != SQL_SUCCESS ) check_error (*henv, *hdbc, SQL_NULL_HSTMT, rc); } return(SQL_SUCCESS); }/* end initialize */ /******************************************************************* ** terminate ** - disconnect ** - free connection handle ** - free environment handle *******************************************************************/ int terminate(SQLHENV henv, SQLHDBC hdbc) { SQLRETURN rc; rc = SQLDisconnect (hdbc); /* disconnect from database */ if (rc != SQL_SUCCESS ) print_error (henv, hdbc, SQL_NULL_HSTMT); rc = SQLFreeConnect (hdbc); /* free connection handle */ if (rc != SQL_SUCCESS ) print_error (henv, hdbc, SQL_NULL_HSTMT); rc = SQLFreeEnv (henv); /* free environment handle */ if (rc != SQL_SUCCESS ) print_error (henv, hdbc, SQL_NULL_HSTMT); return(rc); }/* end terminate */ /******************************************************************* ** - print_error - call SQLError(), display SQLSTATE and message *******************************************************************/ int print_error (SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt) { SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1]; SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1]; SQLINTEGER sqlcode; SQLSMALLINT length; while ( SQLError(henv, hdbc, hstmt, sqlstate, &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1, &length) == SQL_SUCCESS ) { printf("\n **** ERROR *****\n"); printf(" SQLSTATE: %s\n", sqlstate); printf("Native Error Code: %ld\n", sqlcode); printf("%s \n", buffer); }; return ( SQL_ERROR); } /* end print_error */ /******************************************************************* ** - check_error - call print_error(), checks severity of return code *******************************************************************/ int check_error (SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt, SQLRETURN frc) { SQLRETURN rc; print_error(henv, hdbc, hstmt); switch (frc){ case SQL_SUCCESS : break; case SQL_ERROR : case SQL_INVALID_HANDLE: printf("\n ** FATAL ERROR, Attempting to rollback transaction **\n"); rc = SQLTransact(henv, hdbc, SQL_ROLLBACK); if (rc != SQL_SUCCESS) printf("Rollback Failed, Exiting application\n"); else printf("Rollback Successful, Exiting application\n"); terminate(henv, hdbc); exit(frc); break; case SQL_SUCCESS_WITH_INFO : printf("\n ** Warning Message, application continuing\n"); break; case SQL_NO_DATA_FOUND : printf("\n ** No Data Found ** \n"); break; default : printf("\n ** Invalid Return Code ** \n"); printf(" ** Attempting to rollback transaction **\n"); SQLTransact(henv, hdbc, SQL_ROLLBACK); terminate(henv, hdbc); exit(frc); break; } return(SQL_SUCCESS); } /* end check_error */
References
- SQLBindCol - Bind a column to an application variable
- SQLExecute - Execute a statement
- SQLExecDirect - Execute a statement directly
- SQLGetCol - Retrieve one column of a row of the result set
- SQLFetchScroll - Fetch from a scrollable cursor
Parent topic:
DB2 UDB CLI functions