3

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 highest value in Predicted and place it in the 'High' column (IN A SPECIFIC ROW)

========= USING THE FOLLOWING SYNTAX I AM ABLE TO ACHIEVE THIS MANUALLY IN SQL WITH THE 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 attempt to use the above syntax in a Perl script it fails due to the '@" or any variable symbol. Here is the Perl syntax I attempted to overcome the variable issue with no real favourable results. This is true even when placing the @peak variable in the 'execute(@peak) with ? in 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: Global symbol "@peak" requires explicit package name

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

3
  • You shouldn't be calling finish after your execute statements. Just about the only time you need to call finish is if you only want to fetch a subset of the results returned by a SELECT. See the documentation for details. Commented Oct 28, 2014 at 6:35
  • More importantly, the variable $Id_var is in single quotes, so its value won't be interpolated. That means your query will only update rows where Id is the literal string $Id_var, which I doubt is what you want. You can leave off the quotes (you've used a placeholder, so it will quote the value automatically if necessary): $sti->execute($Id_var); Commented Oct 28, 2014 at 6:38
  • 2
    Why did you switch from single quotes on the select statement to double quotes on the update statement? If you have stuck with single quotes then it would have just worked. Commented Oct 28, 2014 at 9:34

2 Answers 2

2

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

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...
Sign up to request clarification or add additional context in comments.

Comments

1

Perl sees @peak as an array. Try referring to it as \@peak. The back slash means interpret next character literally.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.