my @id = (1,2,3,4,6,7,8); my $clause = ""; if (@id) { $clause = "where ID in (" . join(",",@id) . ")"; }; my $statement = "select ID,COLOR from TABLE $clause"; ...