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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:41:29+00:00 2026-05-30T15:41:29+00:00

I have some option-item combination in my application from which the user can choose

  • 0

I have some option-item combination in my application from which the user can choose from, and hence to be queried from SQL Server to fetch some results

Let’s say the user choose combination like:

Option1 - Item1
Option1 - Item2
Option2 - Item1

Database table:

Option1     Item1     Details1    Details2    Details3
Option1     Item2     Details4    Details5    Details6
Option1     Item3     Details7    Details8    Details9
Option1     Item4     Details10   Details11   Details12
...
Option2     Item1     Details13   Details14   Details15

On selecting, the above option-item combination, their corresponding details are to be retrieved.

How do I fetch these details by passing in the Option-Item combination in the query?
I tried something like:

SELECT * FROM tbData 
  WHERE Strategy IN ('Option1','Option2') 
  AND Items IN ('Item1','Item2')

…but this gives the wrong answer.

  • 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-30T15:41:31+00:00Added an answer on May 30, 2026 at 3:41 pm

    You can’t use IN like that. You need to say:

    WHERE (Strategy = 'Option1' AND Items IN ('Item1', 'Item2'))
    OR (Strategy = 'Option2' AND Items IN ('Item1'))
    

    This is because IN on each column separately means “return any rows where strategy is any value in (option 1 or option 2), and items is any value in (item 1 or item 2).” You can’t apply those two concepts separately and expect SQL Server to know that Item2 only applies to Option1.


    Now, another option is to use a table-valued parameter. So you can define a table type as follows in SQL Server:

    CREATE TYPE dbo.Options AS TABLE
    (
        Strategy VARCHAR(32),
        Items    VARCHAR(32)
    );
    

    Populate a DataTable in C# with the selected options, then pass that in. Your query becomes:

    CREATE PROCEDURE dbo.whatever
        @tvp dbo.Options READONLY
    AS
    BEGIN
        SELECT d.* FROM tbData AS d
          INNER JOIN @tvp AS t
          ON d.Strategy = t.Strategy
          AND d.Items = t.Items;
    END
    GO
    

    EDIT adding yet another option.

    If you can pass the set of options into your table as a dual-delimited string, e.g.

    @Options = 'Option1,Item1;Option1,Item2;Option2,Item1'
    

    Then you could use a table-valued function, e.g.

    CREATE FUNCTION dbo.SplitMultiStrings
    (
        @List VARCHAR(MAX),
        @d1   CHAR(1),
        @d2   CHAR(1)
    )
    RETURNS TABLE
    AS
      RETURN 
      (
        SELECT x, y FROM 
        (
          SELECT
            x = i.i.value('(./x/text())[1]', 'varchar(32)'),
            y = i.i.value('(./y/text())[1]', 'varchar(32)')
          FROM (
            SELECT [XML] = CONVERT(XML, '<i><x>' 
              + REPLACE(REPLACE(@List, @d1, '</x><y>'),
              @d2, '</y></i><i><x>') + '</y></i>')
          ) AS x CROSS APPLY [XML].nodes('i') AS i(i)
        ) AS y WHERE x IS NOT NULL AND y IS NOT NULL
      );
    GO
    

    So now you can say:

    CREATE PROCEDURE dbo.whatever
      @Options VARCHAR(MAX)
    AS
    BEGIN
      SET NOCOUNT ON;
    
      BEGIN TRY
        SELECT d.* 
          FROM dbo.SplitMultiStrings(@List, ',', ';') AS o
          INNER JOIN tbData AS d
          ON o.x = d.Strategy AND o.y = d.Items;
      END TRY
      BEGIN CATCH
        RAISERROR('You probably have a trailing ; or other formatting issue.', 11, 1);
      END CATCH
    END
    GO
    

    And then call the stored procedure this way (by passing this comma/semi-colon-delimited string from your application):

    EXEC dbo.whatever @Options = 'Option1,Item1;Option1,Item2;Option2,Item1';
    

    But I still think the TVP option is the way to go.

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

Sidebar

Related Questions

I am using some pre-written JavaScript from polldaddy. They have a JavaScript option which,
I have some doubts regarding In-Application-Billing in android: Can i test it with different
I have some javascript in a repeater item which calls AC_FL_RunContent to load and
I have some historical option prices and I'm trying to determine an implied delta.
I have some code that first selects an option value in a dropdown menu
I have some markup similar to the following: <select> <option selected=selected>Apple</option> <option selected=>Orange</option> </select>
For some reason, after installing the iPhone OS 3.0 SDK, I have no option
I have some options for pricing using radio buttons. When a user selects the
I have some code which reads a file through a form field of type
I have some ASP code that I've inherited from my predecessor (no, it's not

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.