SQLをバインドして実行するソース。(Perl)※抜粋

$sql = <<SQL;
select
count(*)
from
TABLENAME
where
DATETIME >= ? and
DATETIME <= ?
SQL

$sth = $dbh->prepare($sql);

for($i = 0; $i < 100; $i ++) {
  $sth->execute($HASH{'strdate'}, $HASH{'enddate'});
  $result[$i] = $sth->fetchrow_array;
  $sth->finish();
}

ループ外でprepare。
ループ内でexecuteする際に引数で値を渡し、ループ内でfinishするのがミソ。