0
#!/usr/bin/perl
use strict;
use DBI;

use Data::Dumper;
use Asterisk::AGI;
my $agi = new Asterisk::AGI;


my $extension  = '8315';  
my $cti  = '7702009896';  
my $service_id =1;


my $DSN = q/dbi:ODBC:SQLSERVER/;
my $uid = q/ivr/;
my $pwd = q/ivr/;

my $DRIVER = "Freetds";
my $dbh = DBI->connect($DSN,$uid,$pwd) or die "Coudn't Connect SQL";


my $sql3=(qq{
                declare '@' + callnumber as int
                set '@' + callnumber = $callnumber
                set '@' + callnumber = (Select '@'+'@' + identity)

                exec "insert into rpt_call_detail (call_start_time,call_number,call_service_id,call_step_name,call_step_type,call_step_discription) values(getdate(),'@' + callnumber,$service_id,'START',0,'CLI:' + $cli)"

          });


my $call_insert1 = $dbh->prepare($sql3);
$call_insert1->execute();

如何在 Perl 脚本中设置 sql server 变量?我想将电话号码设置为@@identity 我无法成功执行上述代码。请帮助我。

4

1 回答 1

0

declare '@' + callnumber as int不是有效的 (MS) SQL。你想让它说declare @callnumber as int,所以在转义之后@,你最终得到declare \@callnumber as int. 我不熟悉 using @@IDENTITY,但假设这部分是正确的,您的 $sql3 变量将如下所示:

my $sql3=(qq{
                declare \@callnumber as int
                set \@callnumber = $callnumber
                set \@callnumber = (Select \@\@identity)

                exec "insert into rpt_call_detail (call_start_time,call_number,call_service_id,call_step_name,call_step_type,call_step_discription) values(getdate(),'@' + callnumber,$service_id,'START',0,'CLI:' + $cli)"

          });
于 2013-01-03T22:56:46.327 回答