?
Solved

Parameter driven conditional SQL query

Posted on 2010-09-20
3
Medium Priority
?
424 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 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
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…
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…

770 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