while (my $row = $sth->fetchrow_hashref) { for my $db (@{$where{NEW_DB}}) { $row->{Db} = $db; for my $user (@{$where{NEW_USER}}) { $row->{User} = $user; print "INSERT INTO DB "; print join(",", map { "$_:'$row->{$_}'" } keys %{$row}); print ";\n"; } } }