This example shows the API calls ODBC application would make when calling a stored procedure that returns a result set.
Note that in this example the DECLARE CURSOR statement does not have an explicit returnability specified. When there is only a single stored procedure on the call stack, the returnability attribute of RETURN TO CALLER as well as that of RETURN TO CLIENT will make the result set available to the caller of the application. Also note that the stored procedure is defined with a DYNAMIC RESULT SETS clause. For SQL procedures, this clause is required if the stored procedure will be returning result sets.
Defining the stored procedure:
PROCEDURE prod.resset CREATE PROCEDURE prod.resset () LANGUAGE SQL DYNAMIC RESULT SETS 1 BEGIN DECLARE C1 CURSOR FOR SELECT * FROM QIWS.QCUSTCDT; OPEN C1; RETURN; END
: strcpy(stmt,"call prod.resset()"); rc = SQLExecDirect(hstmt,stmt,SQL_NTS); if (rc == SQL_SUCCESS) { // CALL statement has executed successfully. Process the result set. // Get number of result columns for the result set. rc = SQLNumResultCols(hstmt, &wNum); if (rc == SQL_SUCCESS) // Get description of result columns in result set { rc = SQLDescribeCol(hstmt,à); if (rc == SQL_SUCCESS) : { // Bind result columns based on attributes returned // rc = SQLBindCol(hstmt,à); : // FETCH records until EOF is returned rc = SQLFetch(hstmt); while (rc == SQL_SUCCESS) { // process result returned on the SQLFetch : rc = SQLFetch(hstmt); } : } // Close the result set cursor when done with it. rc = SQLFreeStmt(hstmt,SQL_CLOSE); :