Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 908783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:46:15+00:00 2026-05-15T16:46:15+00:00

Problem I am trying to write a stored procedure to select a list of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:46:16+00:00Added an answer on May 15, 2026 at 4:46 pm

    If you are using sql 2005 or newer, you can use the EXCEPT operator to find the news items that contain all the required categories. Or more precisely, find the news items where are no names in the list of categories that were not found in the set of news item category names.

    select NI.NewsID, NI.Post
    from NewsItem NI
    where (@pCategories = '' or
        NOT EXISTS (select EntityNameColumn FROM BuildStringTable(@pCategories) 
            EXCEPT 
                  Select CategoryName FROM NewsItem ni 
                  join NewsItemCategories nic ON ni.NewsID=nic.NewsID
                  join Category c ON c.CategoryID = nic.CategoryID 
            WHERE ni.NewsID=NI.NewsID))
    

    To do this without the EXCEPT operator looks like this:

    where (@pCategories = '' or
        NOT EXISTS (select EntityNameColumn FROM BuildStringTable(@pCategories) ct 
            LEFT OUTER JOIN 
                  (Select CategoryName FROM NewsItem ni 
                  join NewsItemCategories nic ON ni.NewsID=nic.NewsID
                  join Category c ON c.CategoryID = nic.CategoryID
                  WHERE ni.NewsID=NI.NewsID) nicn
            WHERE nicn.CategoryName IS NULL)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a stored procedure that will allow me to write
I am trying to write a stored procedure that takes a table name as
I'm trying to solve the 3n+1 problem and I have a for loop that
I am trying to write some integration tests for some SQL server stored procedures
Using SQL Server 2008 Reporting services: I'm trying to write a report that displays
I am trying to modify a stored procedure I wrote to pull data from
I have a stored procedure that adds an object to the database and returns
I am trying to write a simple stored proc which takes three arguments 'database
I am trying to write a method that will take an IntTree as a
Having a problem trying to create a function, as part of a BizTalk helper

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.