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 992353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:15:15+00:00 2026-05-16T06:15:15+00:00

In SQL Server 2008 I would like to create a stored procedure to select

  • 0

In SQL Server 2008 I would like to create a stored procedure to select specific products, passing their IDs as argument.

But I intend to bring as result a range of products.

This range must have the informed productIds argument value (for example: 1 and 8 and 29 – that means, the range MUST have productId 1 and productId 8 and productId 29).

The unwanted ranges (those like productId 1 and productId 8, or productId 1 and productId 29, or productId 8 and productId 29) won’t be shown as result.

How can I filter those unwanted ranges from my query result?

CREATE PROCEDURE [dbo].[ListProduct]
(
    @categoryId
    @productIds VARCHAR(4000) --Suppose we want to bring ONLY productIds 1 AND 8 AND 29 
)
AS
BEGIN

    SELECT 

    category_id,
    product_id, 
    product_name,
    FROM Product
    WHERE category_id = @category_id
    AND productId = --... is it better to use a function? a cursor?

END

If we have

CategoryId 1    
    ProductId 2    
    ProductId 8
    ProductId 20
    ProductId 29

CategoryId 2    
    ProductId 1    
    ProductId 3
    ProductId 20
    ProductId 29

CategoryId 3    
    ProductId 1    
    ProductId 8
    ProductId 20
    ProductId 29

The result would be

CategoryId 1 | ProductId 1
CategoryId 1 | ProductId 8
CategoryId 1 | ProductId 29
CategoryId 3 | ProductId 1
CategoryId 3 | ProductId 8
CategoryId 3 | ProductId 29

Here is what I’ve got so far:

CREATE TABLE #mylist(product_id int NOT NULL PRIMARY KEY)
INSERT #mylist 
    SELECT Value FROM dbo.fn_split('1,8,29',',')

SELECT category_id,product_id
FROM    Product
WHERE   product_id IN (SELECT product_id FROM #mylist)

But, when there’s a product id missing like 8, this query is showing:

CategoryId 1 | ProductId 1
CategoryId 1 | ProductId 8 
CategoryId 1 | ProductId 29
CategoryId 2 | ProductId 1  --category2 should not be displayed,  
CategoryId 2 | ProductId 29 --  because product id 8 is missing....
CategoryId 3 | ProductId 1
CategoryId 3 | ProductId 8
CategoryId 3 | ProductId 29
  • 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-16T06:15:16+00:00Added an answer on May 16, 2026 at 6:15 am

    Basically, you’re passing in a list of values to use a row level filter

    The authoritative, ubiquitous, and oft-quoted article: “Arrays and List in SQL Server”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008 and I would like to use a sproc to
I have a SQL Server 2008 database where I would like to be able
I have found a fairly insignificant bug in SQL Server 2008. I would like
Okay so I'm writing a SQL Server 2008 Stored Procedure (maintenance script). In doing
I am working on a stored procedure in SQL Server 2008 for resetting an
I have created a little complex stored procedure on SQL Server 2008 R2 and
I'm stepping through a stored procedure in SQL Server Management Studio 2008 (SSMS). The
We have several databases on SQL server. We would like to create 1 new
I would like to save a circle in a sql-server 2008 geography field, using
I have a table named myTable on SQL Server 2008 R2 Express. I would

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.