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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:46:01+00:00 2026-06-07T05:46:01+00:00

This question would be addendum on the last answer in T-SQL stored procedure that

  • 0

This question would be addendum on the last answer in T-SQL stored procedure that accepts multiple Id values

I am passing a few list of ids as a parameter to a stored procedure. Each of them default to null if no data is sent in. For instance, I want food products with ids 1, 2, 5, 7, 20 returned by my stored procedure. I also send in a list of color ids, and production location ids. I am passing in a comma delimited list of these ids. Similar to the last answer in the question referenced above, I create a temp table with the data from each of the parameters. I then want to have a select statement that would be something like this:

SELECT * FROM Candies 
INNER JOIN #TempColors 
    ON Candies.ColorsID = #TempColors.ColorID 
INNER JOIN Locations 
    ON Candies.LocationID = Locations.LocationID

This only works when the parameters are populated and LEFT OUTER JOINS will not filter properly. What is the way to filter while accepting null as a valid parameter?

  • 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-06-07T05:46:02+00:00Added an answer on June 7, 2026 at 5:46 am

    You could use

    some join condition OR @param IS NULL
    

    in your join, it would return all results if a null was supplied – though as far as I can see you don’t specify what behaviour you want when null is passed

    (when I say param I mean temp table column doing this on my phone and it’s not easy ;))

    Edit:

    This one worked for me:

    http://sqlfiddle.com/#!3/c7e85/26

    e.g.

    -- Assume this is your values string which is populating the table
    DECLARE @Values varchar(50)
    
    -- Your code to populate the table here: assume the string is NULL when no values are passed
    INSERT INTO #TempColors BLAH BLAH...
    
    -- Select statement
    SELECT * FROM Candies  
        LEFT JOIN #TempColors  
            ON Candies.ColorsID = #TempColors.ColorID
    WHERE 1 = CASE 
                  WHEN Candies.ColorsID IS NULL AND @Values IS NULL THEN 1
                  WHEN Candies.ColorsID IS NOT NULL AND @Values IS NOT NULL THEN 1
                  ELSE 0
              END
    

    This way the NULLs will be filtered out with a NON-NULL parameter, but kept in for a NULL parameter

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

Sidebar

Related Questions

Before I ask this question I would to apologise because of the fact that
A simple answer to this super simple question would be great! Here is the
I found this question that is discussing what I would like to do, but
My intuitive response to this question would be ,This is so stupid that I
I thought this question would answer my question, but I have applied the following
I realize that this question is impossible to answer absolutely, but I'm only after
I thought this question would have already existed on SO, but then I couldn't
I've never really been poised with this question: But would it be a terrible
This question is based on this thread in Meta . I would like to
This question has two parts. Part 1. Yesterday I had some code which would

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.