How to set variables and process variable for MySQL in Perl Script

HERE IS MY TABLE EXAMPLE:

Id  |   Time     |  Predicted |   Actual |   High
----+------------+------------+----------+---------
1   |   01:00:00 |  100       |    100   |    NULL
2   |   02:00:00 |  200       |    50    |    NULL
3   |   03:00:00 |  150       |    100   |    NULL
4   |   04:00:00 |  180       |    80    |    NULL

      

I want to find the highest value in Predicted and put it in the "High" column (SPECIFIC)

========= USING THE FOLLOWING SYNTAX WHICH I SHOULD ACCESS THIS MAN IN SQL FOLLOWING:

SET @peak = (SELECT MAX (Predicted) FROM table);

UPDATE table SET Peak = @ peak WHERE Id = '1';

Id  |  Time      |  Predicted |    Actual |   High
----+------------+------------+-----------+---------
1   |   01:00:00 |  100       |    100    |   200
2   |   02:00:00 |  200       |    50     |   NULL
3   |   03:00:00 |  150       |    100    |   NULL
4   |   04:00:00 |  180       |    80     |   NULL

=======================================

      

However, when I try to use the above syntax in a Perl script, it fails because of the "@" or any variable character. Here is the Perl syntax I tried to overcome with the variable problem with no real favorable results. This is true even when placing the @peak variable in "execute (@peak) with the" pre-leading syntax "parameter:

my $Id_var= '1';
my $sth = $dbh->prepare( 'set @peak  = (SELECT MAX(Predicted) FROM table)' );
my $sti = $dbh->prepare ( "UPDATE table SET Peak = @peak  WHERE Id = ? " );
$sth->execute;
$sth->finish();
$sti->execute('$Id_var');
$sti->finish();
$dbh->commit or die $DBI::errstr;

      

With the following error: The global symbol "@peak" requires an explicit package name

I would appreciate any help to get this working in my Perl script.

+3


source to share


2 answers


Perl sees @peak as an array. Try referring to it as \ @peak. The backslash literally interprets the next character.



+1


source


You need to escape the @symbal symbol (which denotes an array variable) or use single quotes like

my $sti = $dbh->prepare ( "UPDATE table SET Peak = \@peak WHE...

      



Or use a single quote

my $sti = $dbh->prepare ( 'UPDATE table SET Peak = @peak WHE...

      

+2


source







All Articles