在准备好的声明中重复论证

fra*_*nkc 5 sql perl dbi

考虑一个看起来像这样的查询:


my $query=<<QUERY;

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

Run Code Online (Sandbox Code Playgroud)

假设实际查询确实需要联合并且不能以另一种方式有效地解决.where子句中的变量将始终相同.有没有什么方法可以构造这个,所以我只需要传递1个参数来执行而不是两次传递相同的参数?

Ore*_*reo 5

可以尝试以下操作,我假设您将一个整数传递给 where 子句...

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!
Run Code Online (Sandbox Code Playgroud)


Dav*_*oss 4

您可以使用列表重复运算符。

$sth->execute(($value) x 2);
Run Code Online (Sandbox Code Playgroud)