sandygallant has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a perl script that has multiple database calls and I continuously get the following error: DB-Library error: Attempt to initiate a new SQL Server operation with results pending. In my code I have the following: The first call to the database performs a number of insert statements built from a hash:
while (my ($key, $value) = each(%holidays)) { system("log4k", "DEBUG", "$0", "Staging holiday info data for: $ca +l_name: $key"); $sql = "INSERT INTO stg_holiday_data (hol_mnemonic, hol_date, hol_ +comment, dml_type) VALUES (\"$cal_name\", $key, \"$value\", \"N\") +"; system("log4k", "TRACE", "$0", "SQL being executed: $sql"); if ($test == 0) { $dbh->dbcmd($sql); ($dbh->dbsqlexec() && $dbh->dbresults) or &fatalError("Database error in $sql", "DB_ERROR"); while($dbh->dbresults != NO_MORE_RESULTS) { while(my @dat = $dbh->dbnextrow){} } } }
immediately after that finishes I close off the connection, and cancel it to make sure that there are no results left to be processed by issuing:
$dbh->dbcancel(); $dbh->dbclose();
From there I call a separate subroutine to execute a stored procedure which will produce three lines of output signifying row numbers: subroutine call &run_sproc() if ($test == 0); subroutine:
sub run_sproc() { system("log4k", "DEBUG", "$0", "Loading staged holiday data"); my $sql1 = "upd_holiday_data"; system("log4k", "TRACE", "$0", "SQL being executed: $sql1"); my($dbh2) = new Sybase::DBlib $ENV{DATABASE_USER}, $ENV{DATABASE_P +ASSWORD}, $ENV{DATABASE_SERVER}, "GME_calendar_sync"; &fatalError("Failed to login to database", "DB_LOGIN_ERROR") unles +s ($dbh2); $dbh2->dbcmd($sql1); ($dbh2->dbsqlexec() && $dbh2->dbresults ) or &fatalError ("Database error in $sql", "DB_ERROR"); while ($dbh2->dbresults != NO_MORE_RESULTS) { while (my @d = $dbh2->dbnextrow) { system("log4k", "TRACE", "$0", "Next row being inserted @d +"); } } $dbh2->dbclose(); }
I do have a third SQL block that comes after the stored procedure that works fine with or without this subroutine. What is happening is I am receiving the error mentioned above right before the results from the stored procedure print. I have tried everything I can imagine to make sure that all the results are being processed. A sample of the log output is below:
[Tuesday, 23 October 2012 13:30:02 BST] [DEBUG] gme_process_calendars. +pl: Staging holiday info data for: CA: 20251226 [Tuesday, 23 October 2012 13:30:03 BST] [TRACE] gme_process_calendars. +pl: SQL being executed: INSERT INTO stg_holiday_data (hol_mnemonic, h +ol_date, hol_comment, dml_type) VALUES ("CA", 20251226, "upload", +"N") [Tuesday, 23 October 2012 13:30:03 BST] [DEBUG] gme_process_calendars. +pl: Staging holiday info data for: CA: 20220103 [Tuesday, 23 October 2012 13:30:03 BST] [TRACE] gme_process_calendars. +pl: SQL being executed: INSERT INTO stg_holiday_data (hol_mnemonic, h +ol_date, hol_comment, dml_type) VALUES ("CA", 20220103, "upload", "N" +) [Tuesday, 23 October 2012 13:30:03 BST] [DEBUG] gme_process_calendars. +pl: Loading staged holiday data [Tuesday, 23 October 2012 13:30:03 BST] [TRACE] gme_process_calendars. +pl: SQL being executed: upd_holiday_data DB-Library error: Attempt to initiate a new SQL Server operation with results pendin +g. [Tuesday, 23 October 2012 13:30:03 BST] [TRACE] gme_process_calendars.pl: Next row being inserted 310107230
Any help with this would be greatly appreciated as I have already tried everything I can find doing an internet search and reading the documentation. Thanks Solution Found: Solution Found: In the run_sproc sub routine, the check:
($dbh2->dbsqlexec() && $dbh2->dbresults ) or &fatalError ("Database error in $sql", "DB_ERROR");
the second condition  $dbh2->dbresults is what is causing this error condition, once this was removed the error did not come up anymore. Thank you everyone that looked into this for me

Replies are listed 'Best First'.
Re: DBLib error driving me crazy
by roboticus (Chancellor) on Oct 23, 2012 at 17:59 UTC

    sandygallant:

    I know that SQL server can return multiple datasets. Is it possible that one of your proces is returning an additional dataset that you're not reading? You could check that by preceding the failing statement with a fetch loop, printing out any data you find.

    In any case, you might want to read the docs on the Active method to test whether the the statement handle is still active and finish to terminate the statement if so.

    Caveat: I've not tried this in the last few years, so my recollections could be a bit fuzzy.

    Update: Fixed links to cpan. (I used http://search.cpan.org/~timb/DBI-1.622/DBI.pm#Active, but I don't know if that's the best way to do it. I didn't get anything useful when I tried cpan://DBI#Active.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: DBLib error driving me crazy
by mpeppler (Vicar) on Oct 24, 2012 at 10:54 UTC
    While the DBlib API will still work (for the most part) I strongly recommend that you migrate your code to something that is based on Client Library (i.e. Sybase::CTlib, or DBI/DBD::Sybase).

    The reason: Sybase has not updated DBlib recently, and any changes in the TDS protocol in the 15.x code base is not available when using DBlib (e.g. longer identifier names, for example).

    Michael