Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeated arguments in a prepared statement

Tags:

sql

perl

dbi

consider a query that looks something like this:


my $query=<<QUERY;

select * from foo1 where col < ?
union all
select * from foo2 where col < ?
QUERY

Assume that the actual query really needs unions and can't be efficiently solved another way. The variable in the where clause will always be the same. Is there any way I can structure this so that I only need to pass 1 argument to execute instead of passing the same argument twice?

like image 504
frankc Avatar asked May 10 '12 17:05

frankc


2 Answers

Could try the following, I'm assuming you're passing an integer to the where clause...

DECLARE @variableName as int

SET @variableName = ? --the value gets passed here once

select * from foo1 where col < @variableName -- and gets used here
union all
select * from foo2 where col < @variableName -- and here!
like image 160
Oreo Avatar answered Sep 18 '22 18:09

Oreo


You could use the list repetition operator.

$sth->execute(($value) x 2);
like image 37
Dave Cross Avatar answered Sep 20 '22 18:09

Dave Cross