Solved

Parameter driven conditional SQL query

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…

730 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