$stmt = "SELECT id, name FROM table1"; $sth = execute_it($dbh,$stmt); my $table1 = $sth->fetchall_arrayref({}); my ($i, $j, $sth2); $stmt = "SELECT id, name FROM table2"; $sth = $dbh->prepare($stmt); $sth->execute(); print "Content-type: text/plain \n\n"; my $max_rows = 500; my $rows = []; # cache for batches of rows while ( my $row = ( shift( @$rows ) || shift( @{$rows=$sth->fetchall_arrayref({},$max_rows) || []} ) ) ) { for ( $row ) { for $j ( 0 .. $#$table1 ) { if ( $row->{'name'} eq $table1->[$j]{'name'} ) { $stmt = qq/UPDATE table2 SET name = ? WHERE id = "$row->{id}"/; print "Here.$j".Dumper ($stmt)."\n"; $sth2 = $dbh->prepare($stmt); $sth2->execute ( $table1->[$j]{'id'}); } } } }; close_db(); exit();