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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:41:06+00:00 2026-05-14T19:41:06+00:00

I have a query that works: DECLARE @ProductID int SET @ProductID = ‘1234’ SELECT

  • 0

I have a query that works:

DECLARE @ProductID int
SET @ProductID = '1234'

SELECT DISTINCT TOP 12 a.ProductID
FROM A a
WHERE a.CategoryID IN (SELECT b.CategoryID FROM B b WHERE b.ProductID = @ProductID)
AND a.ProductID != @ProductID

It returns a list of 12 product numbers, all unique.

I need to store these results in a variable, comma separated, because that’s what 3rd party stored procedure needs. So I have this:

 DECLARE @ProductID int
 DECLARE @relatedprods varchar(8000)
 SET @ProductID = '1234'
 SET @relatedprods = ''

 SELECT TOP 12 @relatedprods = @relatedprods + CONVERT(VARCHAR(20), a.ProductID) + ', '
   FROM A a
   WHERE a.CategoryID IN (SELECT b.CategoryID FROM B b WHERE B.ProductID = @ProductID)
   AND a.ProductID != @ProductID

 SELECT @relatedprods

Now, none of these are distinct, but it is returning 12 rows.

Now I add the ‘distinct’ back in, like in the first query:

 DECLARE @ProductID int
 DECLARE @relatedprods varchar(8000)
 SET @ProductID = '1234'
 SET @relatedprods = ''

 SELECT DISTINCT TOP 12 @relatedprods = @relatedprods + CONVERT(VARCHAR(20), a.ProductID) + ', '
   FROM A a
   WHERE a.CategoryID IN (SELECT b.CategoryID FROM B b WHERE B.ProductID = @ProductID)
   AND a.ProductID != @ProductID

 SELECT @relatedprods

Only one product is returned in the comma separated list! Does ‘distinct’ not work in assignment statements? What did I do wrong? Or is there a way to get around this?

Thanks in advance!

CONCLUSION:

I have no idea what causes this problem, though the guess proposed seems logical. I was able to solve this problem via sub-query, and am posting it so others can see the solution:

 DECLARE @ProductID int
 DECLARE @relatedprods varchar(8000)
 SET @ProductID = '1234'
 SET @relatedprods = ''

 SELECT @relatedprods = @relatedprods + CONVERT(VARCHAR(20), c.ProductID) + ',' 
    FROM (SELECT DISTINCT TOP 12 a.ProductID FROM A a WHERE a.CategoryID IN 
      (SELECT b.CategoryID 
      FROM B b 
      WHERE B.ProductID = @ProductID) 
    AND a.ProductID != @ProductID ) c

 SET @relatedprods = SUBSTRING(@relatedprods, 0, LEN(@relatedprods))
 SELECT @relatedprods
  • 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-14T19:41:06+00:00Added an answer on May 14, 2026 at 7:41 pm

    Get the 12 records in a subquery:

    declare
      @ProductID int,
      @relatedprods varchar(8000)
    
    set @ProductID = '1234'
    set @relatedprods = ''
    
    select @relatedprods = @relatedprods + cast(ProductID as varchar) + ','
    from (
      select distinct top 12 a.ProductId
      from A a
      inner join B b on b.CategoryID = a.CategoryID
      where  B.ProductID = @ProductID and a.ProductID != @ProductID
    ) x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query that works well. SELECT DISTINCT city,region1,region2 from static_geo_world where
So I have this query that works perfectly: SELECT users.*, GROUP_CONCAT(categories.category_name) AS categories FROM
I have query that looks like this declare @tmpTable Table(id int) insert into @tmpTable(id)
I have a simple query like so: SELECT * FROM orders that returns all
I am using in C# MYsql .I have query that works if I run
I have this linq query that works well (although it may be written better,
I have written a SQL query that works just fine, but am having a
I have a SQL query that I'm trying to debug. It works fine for
I have a query that successfully grabs the unique products from my products table
I have an ASP SqlDataSource connected to Sybase DB query, using Select Parameters that

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.