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

  • Home
  • SEARCH
  • 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 823681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:59:08+00:00 2026-05-15T02:59:08+00:00

I’m trying to construct a T-SQL statement with a WHERE clause determined by an

  • 0

I’m trying to construct a T-SQL statement with a WHERE clause determined by an input parameter. Something like:

SELECT * FROM table
WHERE id IN
CASE WHEN @param THEN
(1,2,4,5,8)
ELSE
(9,7,3)
END

I’ve tried all combination of moving the IN, CASE etc around that I can think of. Is this (or something like it) possible?

  • 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-15T02:59:09+00:00Added an answer on May 15, 2026 at 2:59 am

    try this:

    SELECT * FROM table
    WHERE (@param='??' AND id IN (1,2,4,5,8))
    OR (@param!='??' AND id in (9,7,3))
    

    this will have a problem using an index.

    The key with a dynamic search conditions is to make sure an index is used, instead of how can I easily reuse code, eliminate duplications in a query, or try to do everything with the same query. Here is a very comprehensive article on how to handle this topic:

    Dynamic Search Conditions in T-SQL by Erland Sommarskog

    It covers all the issues and methods of trying to write queries with multiple optional search conditions. This main thing you need to be concerned with is not the duplication of code, but the use of an index. If your query fails to use an index, it will preform poorly. There are several techniques that can be used, which may or may not allow an index to be used.

    here is the table of contents:

      Introduction
          The Case Study: Searching Orders
          The Northgale Database
       Dynamic SQL
          Introduction
          Using sp_executesql
          Using the CLR
          Using EXEC()
          When Caching Is Not Really What You Want
       Static SQL
          Introduction
          x = @x OR @x IS NULL
          Using IF statements
          Umachandar's Bag of Tricks
          Using Temp Tables
          x = @x AND @x IS NOT NULL
          Handling Complex Conditions
       Hybrid Solutions – Using both Static and Dynamic SQL
          Using Views
          Using Inline Table Functions
       Conclusion
       Feedback and Acknowledgements
       Revision History

    if you are on the proper version of SQL Server 2008, there is an additional technique that can be used, see: Dynamic Search Conditions in T-SQL Version for SQL 2008 (SP1 CU5 and later)

    If you are on that proper release of SQL Server 2008, you can just add OPTION (RECOMPILE) to the query and the local variable’s value at run time is used for the optimizations.

    Consider this, OPTION (RECOMPILE) will take this code (where no index can be used with this mess of ORs):

    WHERE
        (@search1 IS NULL or Column1=@Search1)
        AND (@search2 IS NULL or Column2=@Search2)
        AND (@search3 IS NULL or Column3=@Search3)
    

    and optimize it at run time to be (provided that only @Search2 was passed in with a value):

    WHERE
        Column2=@Search2
    

    and an index can be used (if you have one defined on Column2)

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

Sidebar

Ask A Question

Stats

  • Questions 431k
  • Answers 431k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One thought I have, perhaps not the best way, is… May 15, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer It is not possible to convert any .htc file to… May 15, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer It looks like you're confused about the semantics of XAML.… May 15, 2026 at 2:16 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.