Problem
I am trying to write a stored procedure to select a list of NewsItem records where each NewsItem has all of the Categories that are in a list. If the list is empty then it should return all news items.
NewsItem NewsItemCategories Category
-------- ------------------ --------
NewsID NewsID CategoryID
Post CategoryID CategoryName
I pass a list of comma separated category names to my stored procedure and have created a function that returns a table of these categories.
exec sp_GetNewsItems 'sport,football,hockey'
EntityNameColumn - table returned from my function BuildStringTable
----------------
sport
finance
history
What I Have Tried
select NI.NewsID, NI.Post
from NewsItem NI
where (@pCategories = '' or
(select COUNT(*)
from NewsItemCategories NIC
inner join Category C on NIC.CategoryID = C.CategoryID
inner join BuildStringTable(@pCategories) CT on C.CategoryName = CT.EntityNameColumn
where NIC.NewsID = NI.NewsID) > 0)
Question
The query works if you pass it a single category name, however does not work when you pass multiple category names. In the example query above this should return NewsItems that contain, at least, Categories sport, football, hockey.