# @ins is actually calculated at runtime. # Defined here for demonstration. my @ins = qw(1 2 3 4 5 6); my $in = join ', ', @ins; my $sth = $dbh->prepare("SELECT foo FROM Bar WHERE baz IN ( ? )"); $sth->execute( $in );
Second way:
my @ins = qw(1 2 3 4 5 6); my $in; $in .= '?, ' for @ins; # discard hanging comma $in =~ s/, $//; my $sth = $dbh->prepare("SELECT foo FROM Bar WHERE baz in ( $in )"); $sth->execute( @ins );
From an elegance standpoint, it's nice to have each "IN" item be its own bind parameter, but I don't know if that's best for efficiency. With a single bind parameter, prepare_cached could be used since the rest of the statement doesn't change. On the other hand, I don't know if prepare_cached offers any advantage whatsoever when dealing with this type of query. If it matters, I'm using MySQL with InnoDB tables.
In reply to DBI and variable numbers of bind parameters by friedo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |