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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:21:37+00:00 2026-05-12T13:21:37+00:00

Say you have WIDGETS table: WidgetID int ColorID int (Lookup values: Red, Blue, Green,

  • 0

Say you have WIDGETS table:

  • WidgetID int
  • ColorID int (Lookup values: Red, Blue, Green, Yellow, Black, Brown)
  • SizeID int (Lookup values: Small, Med, Big, Large)
  • Weight int (Lookup values: UltraLight, Light, Normal, Heavy, UltraHeavy

Ok that’s the general idea of the table. I don’t need the lookup names, what I do have is the lookupvalue ID’s. So from this table, I would to be able pull lists back that meet say the following crieria:

  1. Show me all widgets who are Red, or Blue, or Black
  2. Show me all widgets who are Red or Blue, and Small or Med
  3. Show me all widgets who are Heavy only
  4. Show me all widgets who are heavy and Yellow
  5. Show me all widgets who are heavy and yellow and small or big
  6. Show me all widgets who are Large only
  7. Show me all widgets who are Green only

Get the idea? I’ve been trying to work on a Stored Proc that allowed me to send in some type of parameters. Even tried dynamic sql but getting weird errors. can’t remember now.

My Attempt #3, sort of works if I could get it working in a Proc and be able to condition out which Join’s I want

Examples of what I’ve tried:

Note: In my examples: (CourseID is WidgetID) and (StateID is ColorID) and (CreditTypeID is SizeID) and (SubjectID is WeightID)

Attempt Method 1

ALTER PROCEDURE [dbo].[CourseListFullInfoByStateCreditSubject]
  @StateIDs VARCHAR(200) = '',
  @CreditTypeIDs VARCHAR(200) = '',
  @SubjectTypeIDs VARCHAR(200) = ''
AS

BEGIN

  DECLARE @SQL AS NVARCHAR(MAX)
  SET @SQL = 'SELECT DISTINCT
                     C.CourseID,
                     LU.FirstName,
                     LU.LastName,
                     (SELECT COUNT(ReviewID) FROM Review AS R WHERE R.CourseID = C.CourseID) AS ReviewCount
                FROM [Course] AS C WITH(NOLOCK)
                JOIN LexUser AS LU ON LU.LexUserID = C.PresenterID '

If @StateIDs IS NOT NULL AND @StateIDs <> '''' AND @StateIDs <> '0'
BEGIN
  SET @SQL = @SQL + ' JOIN CourseToState AS CS ON CS.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CS.StateID AS VARCHAR) + '','', '','' + @StateIDs + '','') > 0 '
END

If @CreditTypeIDs IS NOT NULL AND @CreditTypeIDs <> '' AND @CreditTypeIDs <> '0'
BEGIN
  SET @SQL = @SQL + 'JOIN CourseToCreditType As CC ON CC.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CC.CreditTypeID AS VARCHAR) + '','', '','' + @CreditTypeIDs + '','') > 0 '
END

If @SubjectTypeIDs IS NOT NULL AND @SubjectTypeIDs <> '' AND @SubjectTypeIDs <> '0'
BEGIN
  SET @SQL = @SQL + 'JOIN CourseToSubject As CSu ON CSu.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CSu.SubjectID AS VARCHAR) + '','', '','' + @SubjectTypeIDs + '','') > 0 '
END

EXEC sp_executesql @SQL

With Attempt 1 I try and send in the ID’s:

[CourseListFullInfoByStateCreditSubject] ''1,2,4'', ''0'', ''0''

…but I get an error, “Incorrect syntax near ‘1’.”

Attempt Method 2, gives same error

DECLARE @SQL AS NVARCHAR(MAX)
SET @SQL = 'SELECT DISTINCT
                   C.CourseID,
                   LU.FirstName,
                   LU.LastName,
                   (SELECT COUNT(ReviewID) 
                     FROM Review AS R 
                    WHERE R.CourseID = C.CourseID) AS ReviewCount
             FROM [Course] AS C WITH(NOLOCK)
             JOIN LexUser AS LU ON LU.LexUserID = C.PresenterID '

If @StateIDs IS NOT NULL AND @StateIDs <> '''' AND @StateIDs <> '0'
BEGIN
  SET @SQL = @SQL + ' JOIN CourseToState AS CS ON CS.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CS.StateID AS VARCHAR) + '','', '','' + @StateIDs + '','') > 0 '
END

If @CreditTypeIDs IS NOT NULL AND @CreditTypeIDs <> '' AND @CreditTypeIDs <> '0'
BEGIN
  SET @SQL = @SQL + 'JOIN CourseToCreditType As CC ON CC.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CC.CreditTypeID AS VARCHAR) + '','', '','' + @CreditTypeIDs + '','') > 0'
END

If @SubjectTypeIDs IS NOT NULL AND @SubjectTypeIDs <> '' AND @SubjectTypeIDs <> '0'
BEGIN
  SET @SQL = @SQL + 'JOIN CourseToSubject As CSu ON CSu.CourseID = C.CourseID AND CHARINDEX('','' + CAST(CSu.SubjectID AS VARCHAR) + '','', '','' + @SubjectTypeIDs + '','') > 0'
END

Attempt Method 3 – Sort of Works
This method only works if I send it ID’s for each feld, I can’t leave one blank to get “all” and I can’t get it working in a stored proc, which is what I tried to do in Attempt #2

DECLARE @StateIDs VARCHAR(200) = ''
DECLARE @CreditTypeIDs VARCHAR(200) = ''
DECLARE @SubjectTypeIDs VARCHAR(200) = ''
SET @StateIDs = '1,3,2,'
SET @CreditTypeIDs = '1,3'
SET @SubjectTypeIDs = '1,2,3,4' 

SELECT DISTINCT
       C.CourseID,
       LU.FirstName,
       LU.LastName,
       (SELECT COUNT(ReviewID) FROM Review AS R WHERE R.CourseID = C.CourseID) AS ReviewCount
  FROM [Course] AS C WITH(NOLOCK)
  JOIN LexUser AS LU ON LU.LexUserID = C.PresenterID 
  JOIN CourseToState AS CS ON CS.CourseID = C.CourseID AND CHARINDEX(',' + CAST(CS.StateID AS VARCHAR) + ',', ',' + @StateIDs + ',') > 0 
  JOIN CourseToCreditType As CC ON CC.CourseID = C.CourseID AND CHARINDEX(',' + CAST(CC.CreditTypeID AS VARCHAR) + ',', ',' + @CreditTypeIDs + ',') > 0
  JOIN CourseToSubject As CSu ON CSu.CourseID = C.CourseID AND CHARINDEX(',' + CAST(CSu.SubjectID AS VARCHAR) + ',', ',' + @SubjectTypeIDs + ',') > 0
  • 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-12T13:21:38+00:00Added an answer on May 12, 2026 at 1:21 pm

    How about something like this:

    Make a stored procedure with a comma-separated list of conditions, should look like

    DECLARE @colorConditions VARCHAR(100)
    SET @colorConditions = ' Red, Blue, Green, Yellow, Black, Brown, '
    
    DECLARE @sizeConditions VARCHAR(100)
    SET @sizeConditions = ' Small, Med, Big, Large, '
    
    DECLARE @weightConditions VARCHAR(100)
    SET @weightConditions = ' UltraLight, Light, Normal, Heavy, UltraHeavy, '
    
    DECLARE @SQL VARCHAR(1000)
    SET @SQL = 
         'SELECT * 
          FROM Widgets W
          INNER JOIN Colors C 
            ON W.ColorID = C.ColorID AND ''' + @colorConditions + ''' LIKE ''% '' + C.Color + '', %'' 
          INNER JOIN Sizes S 
            ON W.SizeID = S.SizeID AND ''' + @sizeConditions + ''' LIKE ''% '' + S.Size + '', %''
          INNER JOIN Weight WT 
            ON W.WeightID = W.WeightID AND ''' + @weightConditions + ''' LIKE '' %'' + WT.Weight + '', %'''
    
    EXEC (@SQL)
    

    Nothing special here. All it does is match each trait (Color, Size, Weight) against a comma-separated list of qualifying conditions. If you want to filter on Blue items, you should pass the string ‘ Blue, ‘. The flanking spaces and commas should be present because it’s easier to have them there then to fight with SQL. The condition string is then compared to each color value like so:

    Condition string: 
    
    ' Blue, Green, '
    
    Colors: 
    
    Red
    Blue
    Green
    
    ...AND ' Blue, Green, ' LIKE '% Red, %' -- fails
    ...AND ' Blue, Green, ' LIKE '% Blue, %' -- succeeds
    ...AND ' Blue, Green, ' LIKE '% Green, %' -- succeeds
    

    This must be executed dynamically, unless you split the conditions out somehow to a temporary table and use the IN operator.

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

Sidebar

Related Questions

In PostgreSQL 8.3, let's say I have a table called widgets with the following:
For simplicity's sake, let's say I have a SQL Server CE table called Widgets
Lets say I have a database of widgets. I am showing a list of
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have some data stored in an audit table, where triggers on the
Let's say I have a gwt application with many widgets and I've used them
I have have a simple HTML form with say four input widgets (see below)...two
Lets say I have a website that sells widgets. I would like to do
Say I have the following table: TABLE: widget - widget_id (not null, unique, auto-increment)
Let's say I have a database of millions of widgets with a price attribute.

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.