my $sth1 = $localDB->prepare("< Some select query that returns more 100000 records (1 column) >"); my $sth2 = $localDB->prepare("< Inner select query that uses the values (id) from the previous sql >"); $sth1->execute(); my ($id); $sth1->bind_columns( undef, \$id); while ( $sth1->fetch() ) { ... $sth2->execute($id); my $dblinkname; while (($dblinkname) = $sth2->fetchrow_array()) { my $dblink_sql = ""); E.G.