2013-01-03 98 views
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變量?我想將callnumber設置爲@@ identity我不能成功執行上面的代碼。請幫助我。如何在perl腳本中執行插入命令?

+1

http://search.cpan.org/dist/DBI/DBI.pm#last_insert_id – alex

+0

我希望將callnumber設置爲@@ IDENTITY它是一個sql server關鍵字,其中@@ IDENTITY: - 標識列的含義通常用於爲表的主鍵生成唯一值。 – user1363308

回答

0

declare '@' + callnumber as int無效(MS)SQL。你想要它代替declare @callnumber as int,所以在轉義@之後,最終會得到declare \@callnumber as int。我不熟悉使用@@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)" 

      });