如何在perl中添加fetch_status

时间:2013-11-18 14:20:28

标签: sql-server perl sql-server-2005 dbi

我有SQL Server 2005的这个代码,它运行正常

    declare @GroupID varchar(11)

    DECLARE ConMaster CURSOR FOR
    SELECT groupid from collectorgroup where (CollectionDate BETWEEN '08/01/2013 00:00:00'  and   '08/01/2013 23:59:59') AND oRnO <> ''

    OPEN ConMaster

         FETCH NEXT FROM ConMaster
         into @GroupID
            WHILE @@FETCH_STATUS = 0
                BEGIN
                exec x_sp_TDCR_Summ @GroupID


         FETCH NEXT FROM ConMaster
         into @GroupID
         END
             CLOSE ConMaster
             DEALLOCATE ConMaster

我希望将其转移到perl脚本,这是我的代码:

 use warnings;
 use DBI;

 my $dbfile = "dbname";
 my $user     = "sa";
 my $password = "";
 my $host = "localhost";
 my $dsn      = "dbi:ODBC:Driver={SQL Server};server=$host;database=$dbfile";
 my $dbh = DBI->connect($dsn, $user, $password,
                   { RaiseError => 1, AutoCommit => 1 }
   ) || die "Error connecting to the database: $DBI::errstr\n";



 print("Enter month (in number):");
 my $mo=<>;
 chomp($mo);
 print("enter date: ");
 my $da=<>;
 chomp($da);
 print("Enter year: ");
 my $yr=<>;
 chomp($yr);

 my $ddate=$yr.'-'.$mo.'-'.$da." "."00:00:00";
 my $ddate2=$yr.'-'.$mo.'-'.$da." "."23:59:59";

 $sql="declare @GroupID varchar(11)

 DECLARE ConMaster CURSOR FOR
   SELECT groupid from collectorgroup where CollectionDate BETWEEN '$ddate'  and  '$ddate2'

 OPEN ConMaster

 FETCH NEXT FROM ConMaster
 into @GroupID
WHILE @@FETCH_STATUS = 0
BEGIN
    exec x_sp_TDCR_Summ @GroupID


    FETCH NEXT FROM ConMaster
    into @GroupID
END
 CLOSE ConMaster
 DEALLOCATE ConMaster";

 my $sth = $dbh->prepare($sql);
 $sth->execute();



 while(my $ref=$sql->fetchrow_arrayref()){
print "$ref->{'description'} $ref->{'amount'}\n";
 }
 $dbh->disconnect;

但是我收到了这个错误:

Possible unintended interpolation of @GroupID in string at proc1.pl line 41.
Possible unintended interpolation of @FETCH_STATUS in string at proc1.pl line 41
.
Name "main::FETCH_STATUS" used only once: possible typo at proc1.pl line 41.
Name "main::hr" used only once: possible typo at proc1.pl line 7.
Name "main::mday" used only once: possible typo at proc1.pl line 7.
Name "main::min" used only once: possible typo at proc1.pl line 7.
Name "main::sec" used only once: possible typo at proc1.pl line 7.
Enter month (in number):08
enter date: 01
Enter year: 2013
DBD::ODBC::st execute failed: [Microsoft][ODBC SQL Server Driver][SQL Server]Lin
e 1: Incorrect syntax near '('. (SQL-42000)
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword
 'WHILE'. (SQL-42000)
[Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the variable '@'. (S
QL-42000)
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword
 'END'. (SQL-42000)
[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepare
d. (SQL-42000) at proc1.pl line 62, <> line 3.
DBD::ODBC::st execute failed: [Microsoft][ODBC SQL Server Driver][SQL Server]Lin
e 1: Incorrect syntax near '('. (SQL-42000)
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword
 'WHILE'. (SQL-42000)
[Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the variable '@'. (S
QL-42000)
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword
 'END'. (SQL-42000)
[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepare
d. (SQL-42000) at proc1.pl line 62, <> line 3.

我的代码是什么才能正常运行?我还计划将我的程序的结果集插入PostgreSQL数据库,但我还没有完成。我还在研究这段代码

1 个答案:

答案 0 :(得分:1)

使用@转义字符串中的\,因为perl想要插入@GroupID数组

 $sql = "declare \@GroupID varchar(11)

您也可以使用单引号或q{}sprintf()代替插值,

 $sql = q{declare @GroupID .. CollectionDate BETWEEN '%s'  and  '%s' ..};
 $sql = sprintf($sql, $ddate, $ddate2);

同时替换:

 my $ref=$sql->fetchrow_arrayref()

my $ref=$sth->fetchrow_arrayref()

检查perldoc DBI以供参考。