Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: DBI Invalid Cursor State when PRINT statements are embedded in SQL

by JamesNC (Chaplain)
on Oct 19, 2004 at 17:29 UTC ( #400600=note: print w/replies, xml ) Need Help??

in reply to DBI Invalid Cursor State when PRINT statements are embedded in SQL

Here is the Solution:
Martin Evans on the dbi-user mailing list was able to finally find the source of this error, here is the text of his reply:

I analysed your log and did the same thing in ODBCTest which used to come with MDAC and it fails in the same way: SQLPrepare(print 's' select count(*) from table print 'e') SQLNumResultCols() returns SQL_ERROR and invalid cursor state. so, I'm afraid, there is no bug in DBD::ODBC and the problem is in the MS SQL Server driver. Interestingly, our ODBC-ODBC Bridge makes you code almost work because it inserts a call to SQLNumParams between the SQLPrepare and the SQLNumResultCols and that puts the MS SQL Server Driver into a different state. I say "almost" because it does appear to hightlight another bug in the MS SQL Server driver later on where SQLMoreResults returns SQL_NO_DATA but a later call to SQLNumResultCols returns 1 column and this persuades DBD::ODBC there is a column and it fails on a call to SQLDescribeCol (because SQLNumResultCols lied and there is no result-set). Your only choice is to take the prints out.

I ended up building DBI 1.45 and DBD::ODBC 1.11 on my machine.
I was still able to achieve the desired result by using a suggestion our DBA made of substituting a SELECT everywhere there is a PRINT and that seems to work nicely. For statements that return multiple result sets I ended up using the following:

#!C:/perl/bin/perl -w #DBI 1.45 #DBD::ODBC 1.11 use DBI; $dbh = DBI->connect("dbi:ODBC:tmpDB", 'username','password', { PrintError => 1, RaiseError =>0, LongReadLen => 65536, odbc_async_exec => 1, odbc_err_handler => \&errSub } ); $command = qq/ select 'start' select name = 'dbi-user' select 'end' /; $sth = $dbh->prepare($command); die $DBI::errstr unless $sth; my $rc = $sth->execute(); do { my @row; while (@row = $sth->fetchrow_array()) { print join(",", @row), "\n"; } } while ($sth->{odbc_more_results}); $dbh->disconnect; sub errSub { my ($state, $msg) = @_; # Strip out all of the driver ID stuff $msg =~ s/^(\[[\w\s]*\])+//; my $err_text .= $msg."\n"; print "Err($state):",$err_text; }
Thanks to all who responded.

  • Comment on Re: DBI Invalid Cursor State when PRINT statements are embedded in SQL
  • Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://400600]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-01-23 05:56 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (191 votes). Check out past polls.