WHERE database NOT LIKE '%tempdb%' AND ( keyword LIKE '%create%' OR keyword LIKE '%delete%' OR keyword LIKE '%insert%' ... ) #### my @patterns = qw/create delete insert update drop/; my $query = "@sqlstatement"; # don't know what you already have in @sqlstatement though $query .= qq{WHERE database NOT LIKE '%tempdb%'; $query .= qq{AND (}; $query .= join " OR ", map { qq{keyword LIKE '%$_%'} } @patterns; $query .= qq{)}; sth=$dbh->prepare( $query ); $sth->execute; while ($data = $sth->fetchrow_arrayref()) { # the query did all of our checking, so we don't need to check anything. print "$data->[3] $data->[9] $data->[10] $data->[13]\n"; }