# before the loop my $sth1 = $dbh->prepare("select d_id, c_id from d where d_id = ?"); my $sth2 = $dbh->prepare("select d_id, c_id from d where d_id = ?"); # Then within the loop $sth1->execute($rec{did1}); $sth2->execute($rec{did2});