Solved

Parameter driven conditional SQL query

Posted on 2010-09-20
3
418 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
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now