Perl预处理语句不使用绑定参数

时间:2017-03-15 15:54:12

标签: sql-server perl dbi

这一切都在Windows 7上使用Strawberry Perl(v 5.16.3)运行到2012 SQL Server。

我在这里做错了什么,我不知道它可能是什么。我可以运行带有参数的sql语句,它很好。但是,如果我将SQL放在语句句柄中并尝试绑定参数,我会收到SQL错误 - 我怀疑因为参数没有绑定。代码:

use strict;
use warnings;
use 5.016;

use DBI;
use Data::Dump qw( dump );

my $dsn = "Driver=SQL Server;Server=servername;Database=databasename;Uid=username;pwd=password";
my $dbh = DBI->connect_cached("dbi:ODBC:$dsn", undef, undef, {
        PrintError => 0,
        RaiseError => 1,
        ShowErrorStatement => 1,
        LongReadLen => 500000,
    }) or print($DBI::errstr);

my $Id = 586058;
# sql with parameter included. 
my $sql = qq~
         SELECT ps.parmId
         FROM table1 ps
            JOIN table3 pd ON pd.parmId = ps.parmId
         WHERE ps.testOccurId = $Id
            AND pd.requiredForPass = 1
         UNION
         SELECT ps.parmId
         FROM table2 ps
            JOIN table3 pd ON pd.parmId = ps.parmId
         WHERE ps.testOccurId = $Id
            AND pd.requiredForPass = 1
         ORDER BY ps.parmId
    ~;
# this works without error
my $arrayRef = $dbh->selectall_arrayref($sql);
print dump($arrayRef);  # get data

# re-write sql with parameter place holders
$sql = qq~
         SELECT ps.parmId
         FROM table1 ps
            JOIN table3 pd ON pd.parmId = ps.parmId
         WHERE ps.testOccurId = ?
            AND pd.requiredForPass = 1
         UNION
         SELECT ps.parmId
         FROM table2 ps
            JOIN table3 pd ON pd.parmId = ps.parmId
         WHERE ps.testOccurId = ?
            AND pd.requiredForPass = 1
         ORDER BY ps.parmId
    ~;
my $sth = $dbh->prepare($sql);

$sth->bind_param(1, $Id);
$sth->bind_param(2, $Id);
# throws SQL error because parameters are not bound to statement.
$arrayRef = $dbh->selectall_arrayref($sth);
# also does not work if we do the following:
# $arrayRef = $dbh->selectall_arrayref($sth, undef, $Id, $Id);
# we crash here and the rest does not get run.
print dump($arrayRef);

exit 0;

我收到的错误如下:

  

DBD :: ODBC :: db selectall_arrayref失败:[Microsoft] [ODBC SQL Server   驱动程序]数值超出范围(SQL-22003)[对于语句"            SELECT ps.parmId            FROM table1 ps               JOIN table3 pd ON pd.parmId = ps.parmId            在哪里ps.testOccurId =?                   AND pd.requiredForPass = 1            联盟            SELECT ps.parmId            FROM table2 ps               JOIN table3 pd ON pd.parmId = ps.parmId            在哪里ps.testOccurId =?                   AND pd.requiredForPass = 1            ORDER BY ps.parmId           "]在bindParametersTest.pl第56行。]

我怀疑SQL Server说'?'超出我们要查询的int列的范围(因为,从技术上讲,它是) - 因此错误。另外,因为相同的值在我运行它之前包含它的第一个sql中工作正常。

我一直在倾注文件而我无法做出错误。我希望另一双眼睛可以指出我的愚蠢错误。

提前感谢您的帮助。

更新

所以我在参数绑定后尝试了这段代码:

$sth->execute();
$arrayRef = $sth->fetchall_arrayref();

它会产生此错误,显示参数:

  

DBD :: ODBC :: st执行失败:[Microsoft] [ODBC SQL Server   驱动程序]数值超出范围(SQL-22003)[对于语句"            SELECT ps.parmId            FROM table1 ps               JOIN table3 pd ON pd.parmId = ps.parmId            在哪里ps.testOccurId =?                   AND pd.requiredForPass = 1            联盟            SELECT ps.parmId            FROM table1 ps               JOIN table3 pd ON pd.parmId = ps.parmId            在哪里ps.testOccurId =?                   AND pd.requiredForPass = 1            ORDER BY ps.parmId           "与ParamValues:1 = 586058,2 = 586058]在bindParametersTest.pl第59行。

我无法弄清楚为什么只有在绑定参数时查询才会失败...

0 个答案:

没有答案