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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:39:51+00:00 2026-05-25T13:39:51+00:00

We have a large-ish query here that has several params, and for each one,

  • 0

We have a large-ish query here that has several params, and for each one, the query only differs by one portion of the where clause, like so:

CASE WHEN @IncludeNames = 1 AND @NameFilter IS NULL THEN

(SELECT blah FROM blahBlah

    INNER JOIN ... 
    INNER JOIN ...
    INNER JOIN ...
    WHERE blahBlah.Id = x.Id)

WHEN @IncludeNames = 1 AND @NameFilter IS NOT NULL THEN

(SELECT blah FROM blahBlah

    INNER JOIN ... 
    INNER JOIN ...
    INNER JOIN ...
    WHERE blahBlah.Id = x.Id
    AND table2.Id = @NameFilter

It goes on like that for several instances, differing only by one condition on the where clause.

Keep in mind this is in the middle of a larger select.

Is there a good way of cleaning this up, without placing it all into one large concatenated sql string and running exec on it, or using something absurd like multiple stored procs per block, as shown here: http://www.developerfusion.com/article/7305/dynamic-search-conditions-in-tsql/7/

Server is SQL Server 2008 R2. TIA!

  • 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-25T13:39:51+00:00Added an answer on May 25, 2026 at 1:39 pm

    Try setting up your query with an option of all or specific values for each clause e.g.

    SELECT x.*
    FROM   x
    WHERE  (x.id = @NameFilter
    OR     @NameFilter is null)
    AND    (x.typeId = @typeFilter
    OR     -1 =  @typeFilter)
    AND    (x.date = @date
    OR     @date is null)
    AND    (x.someStingType = @someStringType
    Or     '' = @someStringType)
    

    This should allow you to concatenate your clauses into a single select statement. Each parameter may apply a filter or have no effect (if set to the default such as null, empty string or -1).

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

Sidebar

Related Questions

In SQL Server 2005, I have a query that involves a bunch of large-ish
I have a large-ish file (4-5 GB compressed) of small messages that I wish
I have a large-ish PHP project (hundreds of files) that I've been brought in
I have a large(ish) COM object that works as the back end of my
I have a large data table that contains a checkbox for each row. I
I have large batches of XHTML files that are manually updated. During the review
I'm developing a website in PHP and I have large JS files that I
I have a large codebase that targetted Flash 7, with a lot of AS2
I have a large database and would like to select table names that have
I have a large-ish web project which is migrating from classic ASP to ASP.NET

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.