0
#!/usr/bin/perl

use DBI;     

$fund=103;
$mobile_number1="7700009896";
$city_address="hello word";

$sql_query3=(qq{ exec "INSERT INTO [192.168.14.28].CommunicationLog.dbo.sms_processedfeeds (pf_Fund,pf_trtype,pf_acno,pf_ihno,pf_Mobile,pf_msgtrtype,pf_msg,pf_entdt) VALUES  ($fund,'CALL',0,NULL,'$mobile_number1','CALL','$city_address',Getdate())"});

my $sql_sms = $dbh->prepare($sql_query3);
$sql_sms->execute();

我收到以下错误:

DBD::ODBC::db prepare failed: [unixODBC][FreeTDS][SQL Server]The identifier that starts with 'INSERT INTO [192.168.14.28].CommunicationLog.dbo.sms_processedfeeds (pf_Fund,pf_trtype,pf_acno,pf_ihno,pf_Mobile,pf_msgtrtype,pf' is too long. Maximum length is 128. (SQL-42000)
[unixODBC][FreeTDS][SQL Server]Statement(s) could not be prepared. (SQL-42000) at Mobile_verification.pl line 8.
Can't call method "execute" on an undefined value at Mobile_verification.pl line 9.
4

2 回答 2

3

您不需要exec语句中的嵌套引号。改用这个

my $mobile_number1_lit = $dbh->quote($mobile_number1);
my $city_address_lit   = $dbh->quote($city_address);
$sql_query3 = <<END_SQL;
INSERT INTO [192.168.14.28].CommunicationLog.dbo.sms_processedfeeds (pf_Fund, pf_trtype, pf_acno, pf_ihno, pf_Mobile, pf_msgtrtype, pf_msg, pf_entdt)
VALUES ($fund, 'CALL', 0, NULL, $mobile_number1_lit, 'CALL', $city_address_lit, Getdate())
END_SQL

my $sql_sms = $dbh->prepare($sql_query3);
$sql_sms->execute;

或者,最好在 中使用占位符prepare并将参数传递给execute,就像这样

$sql_query3 = <<'END_SQL';
INSERT INTO [192.168.14.28].CommunicationLog.dbo.sms_processedfeeds (pf_Fund, pf_trtype, pf_acno, pf_ihno, pf_Mobile, pf_msgtrtype, pf_msg, pf_entdt)
VALUES (?, 'CALL', 0, NULL, ?, 'CALL', ?, Getdate())
END_SQL

my $sql_sms = $dbh->prepare($sql_query3);
$sql_sms->execute($fun, $mobile_number1, $city_address);
于 2012-12-28T16:50:26.073 回答
1

我不熟悉exec,但消息说第一个参数应该是标识符,而不是 SQL 查询。如果您打算使用execSQL 命令,那么您就是在滥用它。

但是你说你想执行一个INSERT,所以也许你根本不想使用EXECUTE。一个INSERT看起来像:

 my $stmt = "
    INSERT INTO [192.168.14.28].CommunicationLog.dbo.sms_processedfeeds (
              pf_Fund,
              pf_trtype,
              pf_acno,
              pf_ihno,
              pf_Mobile,
              pf_msgtrtype,
              pf_msg,
              pf_entdt
           ) VALUES (
              ?,?,?,?,?,?,?,Getdate()
           )
";
my $sth = dbh->prepare($stmt);
$sth->execute(
   $fund,
   'CALL',
   0,
   undef,
   '$mobile_number1',
   'CALL',
   $city_address,
);

注意:您可以将prepare+execute替换为$dbh->do($stmt, undef, ...data...);

注意:我假设[192.168.14.28].CommunicationLog.dbo.sms_processedfeeds是一个有效的表名称。

于 2012-12-28T16:54:25.010 回答