>declare @KeyWordsCount as int >select @KeyWordsCount = count(*) from Keywords >with KeywordSearchCTE >as >(select * from myTable M INNER JOIN Keywords K on M.FieldToSearch like '%' + K.Search_Word + '%' > UNION ALL > select * from myTable M INNER JOIN Keywords K on M.FieldToSearch like '%' + K.Search_Word + '%' where K.Search_Word > NOT IN (select Search_Word from KeywordSearchCTE)) > >select M.FieldToSearch, count(*) as cnt_Occurs from myTable M >inner join KeywordSearchCTE K on M.ID = K.ID group by 1 having cnt_Occurs = @KeyWordsCount > >>
declare @KeyWordsCount as int select @KeyWordsCount = count(*) from Keywords SELECT MyTable.* FROM MyTable INNER JOIN (SELECT MyTable.PK FROM MyTable INNER JOIN KeyWords ON M.FieldToSerach LIKE '%'+KeyWords.Search_Word +'%' GROUP BY MyTable.Pk HAVING COUNT(*) = @KeyWordsCount) Tbl1 ON MyTable.Pk = Tbl1.Pk