[freetds] more on SQLMoreResults

James K. Lowden jklowden at schemamania.org
Thu Jul 13 00:47:41 EDT 2006

"A result is something returned by the data source after a statement is
executed. ODBC has two types of results: result sets and row counts. Row
counts are the number of rows affected by an update, delete, or insert

This idea of row-count-only resultsets is a snakepit.  It's one of those
areas where the "standard" is whatever the driver says it is.  


do {
	while (my (@row) = $sth1->fetchrow_array) {
	    print "\t", join (", ", @row) , "\n";
} while ($sth1->{odbc_more_results}); 

Hmph!  I've been working with $sth->{Active}, which the DBI doc even says
is poorly defined:

	'The "Active" attribute is true if the handle object is "active". This is
rarely used in applications. The exact meaning of active is somewhat vague
at the moment.'

But then I see $sth->{odbc_more_results}, above.  

	"To determine whether to return true or false, odbc_more_results calls
SQLMoreResults. There are some situations where DBD::ODBC will
automatically call SQLMoreResults without the need for odbc_more_results.
If the previous SQL statement that executed was a non result-set
generating statement, such as an INSERT statement, DBD::ODBC calls
SQLMoreResults for you. This triggers the execution of the next statement
in the procedure. If the previous statement generated a result set, your
script needs to call SQLMoreResults explicitly by using

(Everyone except the ODBC documentation understands "resultset" to mean
"rows of data from the server".)  

Sounds like odbc_more_results exists to process DONEINPROC packets, so
Perl scripts can bypass them.  

For comparison, look at what DB2 says:


	"The ODBC specification of SQLMoreResults() also allow counts associated
with the execution of parameterized INSERT, UPDATE, and DELETE statements
with arrays of input parameter values to be returned. However, DB2 CLI
does not support the return of such count information."

I particularly like this:


	"If a batch of SQL statements is executed, the count of affected rows
might be a total count for all statements in the batch or individual
counts for each statement in the batch."

I wasn't able to find online documentation for Oracle or Sybase.  

I suppose, as best we can, we should emulate Microsoft's implementation:
SQLMoreResults should SUCCEED for DONE packets, so applications can get
rowcounts.  But a standards lawyer could defend almost anything.  


More information about the FreeTDS mailing list