在 Sqlserver 上使用 Perl DBI (DBD::ODBC) 无法获取错误代码、类型和状态

发布于 2024-10-26 22:03:44 字数 844 浏览 1 评论 0 原文

我有一个 perl 脚本,它执行以下操作来运行插入 stmnt 并执行有意的 RAISERROR。现在我可以看到错误已生成,但有错误的错误代码 - SQL-42000 ,但错误诊断方法不返回任何内容。使用 $DBI::err,$DBI::errstr,$DBI::state 也不会打印正确的内容。只有 $DBI::errstr 正确出现。

$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 1;

$dbh->begin_work;
eval {
      $dbh->do(INSERT INTO ..);
      $dbh->do (RAISERROR ('User defined error',16,1) --purposely raising error
      $dbh->commit;
      1;
};
     if ($@) {
     print $@;
     print "err() ==>".$dbh->err();
     print "errstr() ==>".$dbh->errstr();
     print "state() ==>".$dbh->state();
     $dbh->rollback or warn "rollback failed";
}

输出:

DBD::ODBC::db do failed: [unixODBC][FreeTDS][SQL Server]User defined error (SQL-42000) ..
err() =>
errstr() =>
state() =>

I have a perl script which does the below to just run a insert stmnt and doing a deliberate RAISERROR. Now i can see that the error is generated but has the wrong error code - SQL-42000 , but the error diagnostic methods don't return anything. Using $DBI::err,$DBI::errstr,$DBI::state also doesnt print the correct thing. Only $DBI::errstr is coming up correctly.

$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 1;

$dbh->begin_work;
eval {
      $dbh->do(INSERT INTO ..);
      $dbh->do (RAISERROR ('User defined error',16,1) --purposely raising error
      $dbh->commit;
      1;
};
     if ($@) {
     print $@;
     print "err() ==>".$dbh->err();
     print "errstr() ==>".$dbh->errstr();
     print "state() ==>".$dbh->state();
     $dbh->rollback or warn "rollback failed";
}

Output:

DBD::ODBC::db do failed: [unixODBC][FreeTDS][SQL Server]User defined error (SQL-42000) ..
err() =>
errstr() =>
state() =>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋风の叶未落 2024-11-02 22:03:44

您的代码示例已损坏( --thoughtly 和缺少 ;, do call 中缺少引号),并且错误 42000 是因为您调用 raiserror 时出现错误,即“附近的语法不正确...”。我无法判断,因为你确实展示了真正的工作代码。无论如何,您的代码重写为以下内容对我有用:

use DBI;
use strict;
use warnings;

my $dbh = DBI->connect('dbi:ODBC:xxx','xx','xx');

$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 1;

$dbh->begin_work;
eval {
      #$dbh->do(INSERT INTO ..);
      $dbh->do (q/RAISERROR ('User defined error',16,1)/); #--purposely raising error
      $dbh->commit;
      1;
};
     if ($@) {
     print $@;
     print "err() ==>".$dbh->err();
     print "errstr() ==>".$dbh->errstr();
     print "state() ==>".$dbh->state();
     $dbh->rollback or warn "rollback failed";
}

并输出:

DBD::ODBC::db do failed: [unixODBC][Easysoft][SQL Server Driver][SQL Server]User defined error (SQL-42000) at so1.pl line 16.
DBD::ODBC::db do failed: [unixODBC][Easysoft][SQL Server Driver][SQL Server]User defined error (SQL-42000) at so1.pl line 16.
err() ==>1errstr() ==>[unixODBC][Easysoft][SQL Server Driver][SQL Server]User defined error (SQL-42000)state() ==>42000

Your code sample is broken (the --purposely and missing ;, missing quotes in do call) and the error 42000 is because you have an error calling raiserror which is "Incorrect syntax near ...". I can't tell because you do show real working code. Anyway your code rewritten as the following works for me:

use DBI;
use strict;
use warnings;

my $dbh = DBI->connect('dbi:ODBC:xxx','xx','xx');

$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 1;

$dbh->begin_work;
eval {
      #$dbh->do(INSERT INTO ..);
      $dbh->do (q/RAISERROR ('User defined error',16,1)/); #--purposely raising error
      $dbh->commit;
      1;
};
     if ($@) {
     print $@;
     print "err() ==>".$dbh->err();
     print "errstr() ==>".$dbh->errstr();
     print "state() ==>".$dbh->state();
     $dbh->rollback or warn "rollback failed";
}

and outputs:

DBD::ODBC::db do failed: [unixODBC][Easysoft][SQL Server Driver][SQL Server]User defined error (SQL-42000) at so1.pl line 16.
DBD::ODBC::db do failed: [unixODBC][Easysoft][SQL Server Driver][SQL Server]User defined error (SQL-42000) at so1.pl line 16.
err() ==>1errstr() ==>[unixODBC][Easysoft][SQL Server Driver][SQL Server]User defined error (SQL-42000)state() ==>42000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文