#!/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.
答案 0 :(得分: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);
答案 1 :(得分:1)
我不熟悉exec
,但是消息说第一个参数应该是标识符,而不是SQL查询。如果您打算使用exec
SQL命令,则会误用它。
但是你说你想要执行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
是有效的表格名称。