DBLib错误让我发疯

时间:2012-10-23 12:42:50

标签: database perl

我正在处理一个有多个数据库调用的perl脚本,并且我不断收到以下错误:

DB-Library错误:         尝试启动具有待处理结果的新SQL Server操作。

在我的代码中,我有以下内容:

对数据库的第一次调用执行了许多根据哈希构建的插入语句:

while (my ($key, $value) = each(%holidays)) {

    system("log4k", "DEBUG", "$0", "Staging holiday info data for: $cal_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){}
        }
    }
}

在完成后立即关闭连接,取消连接以确保没有结果可以通过发出来处理:

$dbh->dbcancel();
$dbh->dbclose();

从那里我调用一个单独的子程序来执行一个存储过程,它将产生三行输出,表示行号:

子程序调用

&run_sproc() if ($test == 0);

子程序:

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_PASSWORD}, $ENV{DATABASE_SERVER}, "GME_calendar_sync";
    &fatalError("Failed to login imagine database", "DB_LOGIN_ERROR") unless ($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();
}

我确实有一个第三个SQL块,它存储在没有这个子例程的存储过程之后。

发生的事情是我在存储过程打印结果之前收到上述错误。我已经尝试了所有我能想象的东西,以确保所有结果都得到处理。日志输出的示例如下:

[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, hol_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, hol_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 pending.
[Tuesday, 23 October 2012 13:30:03 BST] [TRACE] gme_process_calendars.pl: Next row being inserted 310107230

任何有关此方面的帮助都将非常感激,因为我已经尝试了一些我可以在互联网上搜索和阅读文档的内容。

谢谢

2 个答案:

答案 0 :(得分:0)

我认为您的问题是您的run_proc方法尝试执行此SQL:

my $sql1 = "upd_holiday_data";

我怀疑它是一个有效的SQL命令

答案 1 :(得分:0)

找到解决方案:

在run_sproc子例程中,检查:

 ($dbh2->dbsqlexec() && $dbh2->dbresults ) 
        or &fatalError ("Database error in $sql", "DB_ERROR");

第二个条件$ dbh2-> dbresults是导致此错误情况的原因,一旦删除该错误就不再出现了。