Solved

Parameter driven conditional SQL query

Posted on 2010-09-20
3
423 Views
Last Modified: 2012-05-10
I have an ASP.NET front end app that, at one point, generates Crystal Reports for client orders.
This reporting has just been extended to give the user the ability to filter the results.

Previously this data was filtered by date only. In crystal this query was something like:

select * from orders
where ordr_conf_d between '2010-09-01' and '2010-09-30'

The user will now need to see client orders only for a selected region.
By adding a region dropdown to this, the query will need to be updated to something like this:

select * from orders
where ordr_conf_d between '2010-09-01' and '2010-09-30'
and regn_i = 5

If no region is selected the region id will default to 0 and I would like to run the first query.
How do I achieve this? The following is not possible.

select * from orders
where ordr_conf_d between '2010-09-01' and '2010-09-30'
and regn_i IN (case when @RegionID > 0
                     then @RegionID
               else (select regn_i from lkup_regn)
               end)
0
Comment
Question by:dbasplus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Accepted Solution

by:
aelliso3 earned 500 total points
ID: 33721841
In the code below, if @RegionID  = 0 then it will just skip the regn_i because of the OR statement in there
select * from orders
where ordr_conf_d between '2010-09-01' and '2010-09-30'
      and (regn_i = 5
           or @RegionID = 0)

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33721861
select * from orders
where ordr_conf_d between '2010-09-01' and '2010-09-30'
and (@RegionID = 0 or regn_id = @RegionID)
0
 

Author Closing Comment

by:dbasplus
ID: 33721864
Brilliant and simple, just what I needed.

This is the final result:

select * from orders
where ordr_crte_d between '2010-08-01' and '2010-08-31'
and (clnt_i = @ClientID or @ClientID = 0)
and (regn_i = @RegionID or @RegionID = 0)
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question