我有这样一个查询此执行在Perl程序查询
set @valid_total:=0;
set @invalid_total:=0;
select week as weekno, measured_week,project_id as project,
role_category_id as role_category,
valid_count,valid_tickets,
(@valid_total := @valid_total + valid_count) as valid_total,
invalid_count,invalid_tickets,
(@invalid_total := @invalid_total + invalid_count) as invalid_total
from metric_fault_bug_project
where measured_week = yearweek(curdate())
and role_category_id = 1 and project_id = 11;
它执行海蒂(MySQL客户端)的罚款,但是当涉及到perl,它给了我这个错误
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the m
anual that corresponds to your MySQL server version for the right syntax to use
near ':=0;
set :=0;
select week as weekno, measured_week,project_id
as project' at line 1 at D:\Mx\scripts\test.pl line 35.
Can't execute SQL statement: You have an error in your SQL syntax; check the man
ual that corresponds to your MySQL server version for the right syntax to use ne
ar ':=0;
set :=0;
select week as weekno, measured_week,project_id
as project' at line 1
的问题似乎在set @valid_total := 0;
行。
我对Perl相当陌生。谁能帮忙?
这是完整的Perl代码
#!/usr/bin/perl
#use lib '/x01/home/kalpag/libs';
use DBI;
use strict;
use warnings;
my $sid = 'issues';
my $user = 'root';
my $passwd = 'kalpa';
my $connection = "DBI:mysql:database=$sid;host=localhost";
my $dbhh = DBI->connect($connection, $user, $passwd) ||
die "Database connection not made: $DBI::errstr";
my $sql_query = 'set @valid_total:=0;
set @invalid_total:=0;
select week as weekno, measured_week,project_id,
role_category_id as role_category,
valid_count,valid_tickets,
(@valid_total := @valid_total + valid_count) as valid_total,
invalid_count,invalid_tickets,
(@invalid_total := @invalid_total + invalid_count) as invalid_total
from metric_fault_bug_project
where measured_week = yearweek(curdate())
and role_category_id = 1 and project_id = 11';
my $sth = $dbhh->prepare($sql_query) or die "Can't prepare SQL statement: $DBI::errstr\n";
$sth->execute() or die "Can't execute SQL statement: $DBI::errstr\n";
while (my @memory = $sth->fetchrow())
{
print "@memory \n";
}
我怀疑它可能与@符号有关。它似乎已经失去了变量名称。你有没有试过逃离@标志? – Soz
感谢soz,是的,我已经尝试使用\ @variable_name。但它不起作用:( – KalpaG
请确保你在查询周围使用单引号,使用双引号将导致Perl尝试使用变量插值来处理任何带有@或$ sigils的内容。 – Unixmonkey