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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:52:46+00:00 2026-06-15T21:52:46+00:00

This one is a little hard for me to explain correctly so please bear

  • 0

This one is a little hard for me to explain correctly so please bear with me.

I am trying to create a dynamic SQL query to allow users to input data for various parameters i am creating for the query. I have it running if all the parameters are filled in but i cannot figure out how to allow for one or any of these parameters to allow a null value and still return data based on the other parameters. For example, using the query info below, if i set the @Client Parameter to NULL, iwould want the same info to return for all clients instead of specifying 1 client. I would want a similar function for all of the other parameters except the date fields, as those would always be required.

The reason i want to allow for null values is the final product of this query, i am writing a report using Telerik in VS 2010 which will be deployed for my company to use. In telerik you can allow a parameter to be null at runtime. The goal is to make this report useful for various situations, basically making it so i dont have to write 100 different queries over the next year and also giving the user more control/access over what they need.

Is this possible in SQL?

here is what i have right now:

 DECLARE @Product int
    DECLARE @OrderDate1 datetime
    DECLARE @OrderDate2 datetime
    DECLARE @Status int
    DECLARE @Queue int
    DECLARE @Client int

    SET @Product = 86
    SET @OrderDate1 = '2012-09-01'
    SET @OrderDate2 = '2012-11-30'
    SET @Status = 80
    SET @Queue = 0
    SET @Client = 56156

    SELECT 
    CAST(oi.OrderID AS VARCHAR(MAX))+'.'+CAST(oi.OrderItemID AS VARCHAR(MAX)) AS OrderNumber
    ,CASE WHEN p.Abbreviation IS NULL THEN NULL
        ELSE p.Abbreviation END AS Product
    ,o.OrderDate
    ,v.ContactFirstName+' '+v.ContactLastName AS Vendor
    ,m.Description AS Status
    ,q.Description AS Queue

    FROM
    OrderItems oi
    JOIN Orders o (NOLOCK) ON o.OrderID = oi.OrderID
    JOIN Products p (NOLOCK) ON p.ProductID = oi.ProductID
    JOIN Vendors v (NOLOCK) ON v.VendorID = oi.VendorID
    JOIN Milestones m (NOLOCK) ON m.MilestoneID = oi.LastMilestoneID
    JOIN Queues q (NOLOCK) ON q.QueueID = oi.QueueID

    WHERE
    oi.ProductID in (@Product)
    and o.OrderDate BETWEEN @OrderDate1 and DATEADD(DD, 1, @OrderDate2)
    and oi.LastMilestoneID in (@Status)
    and oi.QueueID in (@Queue)
    and o.ClientID in (@Client)

DDL:

  CREATE TABLE OrderItems
(
OrderID int,
OrderItemID int,
ProductID int,
VendorID int,
LastMilestoneID int,
QueueID int
)
insert into OrderItems
Values(1234567, 1, 86, 105111, 80, 0)
CREATE TABLE Orders
(
OrderID int,
ClientID int,
OrderDate datetime
)
insert into orders
Values(1234567, 56156, '2012-11-08')
CREATE TABLE Products
(
ProductID int,
Abbreviation Varchar(20),

)
insert into products
Values(86, 'Product1')
CREATE TABLE Vendors
(
VendorID int,
ContactFirstName Varchar(20),
ContactLastName Varchar(20)
)
insert into vendors
Values(105111, 'john', 'doe')
 CREATE TABLE Milestones
 (
MilestoneID int,
Description Varchar(20)
)
insert into milestones
values(80, 'Status 1')
CREATE TABLE Queues
(
QueueID int,
Description Varchar(20)
)
insert into Queues
values(0, 'Queue1')
  • 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-15T21:52:47+00:00Added an answer on June 15, 2026 at 9:52 pm

    Since the parameters are single values, I’d use = rather than IN. A very simple approach which will work poorly for large tables is:

    WHERE (@Product IS NULL OR oi.ProductID = @Product)
      AND (@Status IS NULL OR oi.LastMilestoneID = @Status)
      ... etc
    

    What you are trying to perform is what Erland Sommarskog terms as “dynamic search conditions”. Read this article for all your options and the benefits/downsides of each.

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

Sidebar

Related Questions

A little hard to explain in SQL terms because I am using an in-house
I think this is a little hard to explain. Basically, I have an android
Okay, this is a little hard to explain, as the title might suggest. I
This is a little hard to explain, but I have an HTML table, full
This is a little bit of a hard problem to explain, so ill show
This one is a little tricky. Say I have this XmlDocument <Object> <Property1>1</Property1> <Property2>2</Property2>
This one is a little confusing to me. I'm running a LAMP server with
This one might be a little confusing. I'm using AMCharts with rails. Amcharts comes
This one has proven to be a little tricky for me so far. I
This one is a huge issue: first off, while I know a little bit

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.