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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:35:35+00:00 2026-05-28T05:35:35+00:00

I have a table Tran with transaction records. I have two tables, Parameters1 and

  • 0

I have a table Tran with transaction records.

I have two tables, Parameters1 and Parameters2, which are used to filter results when selecting from the Tran table a la:

SELECT *  
FROM  
     Tran t JOIN  
     Parameters1 p1 ON T.code1 = p1.code1  
     Parameters2 p2 ON T.code2 = p2.code2  
WHERE p1.code1 is not null AND
      p2.code2 is not null

Sometimes Parameters1 or Parameters2 is empty.

What I want is for the filtering to happen only if the Parameters tables have records. In other words, if Parameters1 is empty, don’t use it to filter, and vice versa. Same goes for Parameters2.

I’m stumped.

  • 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-28T05:35:35+00:00Added an answer on May 28, 2026 at 5:35 am

    You’d normally do this using dynamic SQL, since coding up dynamic search conditions with static SQL is generally hackish. If you are interested in this approach I highly recommend Erland Sommarskog’s page on dynamic SQL for SQL Server 2005. He has a great case study with plenty of example code on how to do exactly what you’re asking for.

    1. Setup

    Whether you use dynamic or static SQL, you should first determine upfront what filters apply:

    DECLARE @p1_filter BIT;
    DECLARE @p2_filter BIT;
    
    SET @p1_filter = 0;
    SET @p2_filter = 0;
    
    IF EXISTS (SELECT TOP (1) * FROM Parameters1)
    BEGIN
       SET @p1_filter = 1;
    END;
    
    IF EXISTS (SELECT TOP (1) * FROM Parameters2)
    BEGIN
       SET @p2_filter = 1;
    END;
    

    2.a. Dynamic SQL

    If you’re going the dynamic SQL route you’d then do:

    DECLARE @sql NVARCHAR(MAX);
    
    SET @sql = 'SELECT * FROM Tran T ';
    
    IF (@p1_filter = 1)
    BEGIN
       SET @sql = @sql + 'INNER JOIN Parameters1 p1 ON T.code1 = p1.code1 ';
    END;
    
    IF (@p2_filter = 1)
    BEGIN
       SET @sql = @sql + 'INNER JOIN Parameters2 p2 ON T.code2 = p2.code2 ';
    END;
    
    EXECUTE sp_executesql
       @statement = @sql
    ;
    

    2.b.i. Static SQL: Sub-SELECTs in the WHERE clause

    Otherwise, you’d continue with this ugly (and probably poorly performing) static SQL:

    SELECT *
    FROM Tran T
    WHERE 
       (
             T.code1 IN (SELECT code1 FROM Parameters1)
          OR (@p1_filter = 0)
       )
       AND
       (
             T.code2 IN (SELECT code2 FROM Parameters2)
          OR (@p2_filter = 0)
       )
    ;
    

    Erland also presents static SQL approaches like this one on that same page I referred to earlier. You may find a better approach there.

    2.b.ii. Static SQL: LEFT OUTER JOIN

    You can also go with this approach, which you proposed in the comments on this answer:

    SELECT *
    FROM 
                       Tran T
       LEFT OUTER JOIN Parameters1 p1
          ON T.code1 = p1.code1
       LEFT OUTER JOIN Parameters2 p2
          ON T.code2 = p2.code2
    WHERE
           T.code1 = 
              CASE
                 WHEN @p1_filter = 1
                    THEN p1.code1
                 ELSE T.code1
              END
       AND T.code2 =
              CASE
                 WHEN @p2_filter = 1
                    THEN p2.code2
                 ELSE T.code2
              END
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a transaction table which has start_date and end_date of datetime datatype. I
I have two tables kinda like this: // Person.Details Table: PersonID int [PK] |
I have two tables, let's just call them A & B, which can be
I have table rows of data in html being filled from a CGI application.
I have a single transaction table with acc_nbr, tran_nbr, tran_amt, tran_dt, and tran_typ. I
I have many .NET processes reading messages from an SQL Server 2008 DB table
I ran a query to copy 18 million records from one table to another.
I have a rather simple command which I occasionally run: BEGIN TRAN T1; truncate
I have the following database tables/EF objects public class Transaction { //some other properties
I have table with two SelectOneMenu in row. I need populate data in second

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.