INF: Result Processing for SQL Server

ID: Q165951


The information in this article applies to:


SUMMARY

When using Microsoft DB-Library, VBSQL (VBX and OCX), or ODBC to access SQL Server, it is critical that all result sets are completely processed in a timely manner. The result sets need to be processed to avoid problems with subsequent SQL Server queries and to avoid concurrency issues with SQL Server resources.

In most cases the return code from dbsqlok or dbsqlexec should be ignored. If you send the following batch:


   insert into tblTest values(1)
   select @@VERSION 

and the insert statement fails due to a duplicate key, a Sev 14 error is generated but the batch continues. THe dbsqlok and dbsqlexec calls only check the success of the first command. If you do not call dbresults you will not process the select statement results and can get result pending errors.


MORE INFORMATION

The following are the most common problems your application may encounter if result sets are not handled immediately and completely:

Below is an example of how to correctly handle all result sets. This is a C/C++ DB-Library code fragment. The same concept should be applied to VBSQL and ODBC result handling.

   BOOL bMoreResults = TRUE;
   BOOL bMoreRows = TRUE;
   RETCODE dbRC = SUCCEED;

   // 
   // send query
   .
   .
   .
   // 
   // process *all* results

   bMoreResults = TRUE
 while(bMoreResults)
   {
      switch(dbRC = dbresults(pdbproc))
      {
      case SUCCEED:
        bMoreRows = TRUE;
        while(bMoreRows)
        {
          switch(dbRC = dbnextrow(pdbproc))
          {
            case REG_ROW:
              // handle regular row
              break;

            case NO_MORE_ROWS:
              bMoreRows = FALSE;  // all rows in this result set handled
              break;

            case BUF_FULL:
              // handle full buffer when using row buffering
              break;

            case FAIL:
              // any error processing desired
              bMoreRows = FALSE;
              break;

            default:
              // handle compute row
              break;
          }
        }
        break;

      case NO_MORE_RESULTS:
        bMoreResults = FALSE;  // all result sets handled
        break;

      case FAIL:
        // any error processing desired
        // The current command has returned an error
        // could be a non fatal error
        bMoreResults = TRUE;
        break;

      case NO_MORE_RPC_RESULTS:
        // extract stored procedure return information
        break;

      default:
        bMoreResults = FALSE;  // unknown
        break;
      }
   } // while(bMoreResults && FALSE == DBDEAD(pdbproc)) 


Keywords          : kbprg kbusage SSrvProg 
Version           : 4.21a 6.0 6.5
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 9, 1999