foreach $comp_id (keys %hash){ $sql = <fetchrow) { if($data[0]){ print "insert $comp_id in table TYPES for $data[0]\n"; } } }