繁体   English   中英

在SQLServer上未使用Perl DBI(DBD :: ODBC)获得错误代码,类型和状态

[英]Not getting err code,type and state with Perl DBI (DBD::ODBC) on Sqlserver

我有一个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() =>

您的代码示例已损坏(-有目的并且缺少;,在do调用中缺少引号),错误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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM