# Recommend not using '*' but specifying the field names, # to future-proof code. my $query1 = "SELECT * FROM database1.table WHERE ( id = ? );"; my $query2 = "INSERT INTO database2.table ( value1, value2 ) VALUES ( ?, ? ) ;"; my $sth1 = $dbh1->prepare( $query1 ) or die $dbh1->errstr; my $sth2 = $dbh2->prepare( $query2 ) or die $dbh2->errstr; foreach my $id ( @big_data_arrray ) { # Original code had an error # $sth2->execute was called in both cases $sth1->execute( $id ) or die $dbh1->errstr; while ( my @row = $sth1->fetchrow_array ) { $sth2->execute( @row[0 .. 1] ) or die $dbh2->errstr; } $sth1->finish; }