473,388 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Conditionals in a WHERE clause?

prn
254 Expert 100+
Hi folks,

This may sound odd, but I'm preparing a stored procedure that I want to handle a couple of different cases. However, unlike the standard sort of situation where what I want to differentiate is the result, i.e., the selections, what I want to cover is the selection conditions.

I have basically two queries that differ only in a single condition, but I'd like to put them into a single stored proc and parameterize them (much more maintainable). I've boiled this down to the bare essentials, ignoring all the selected fields and the joined tables. The only difference is whether I use [condition1] or [condition2] in the WHERE clause. What I'd like is something like:

Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE get_fubar
  2. (
  3.     @mystring VARCHAR(20),
  4.     @type CHAR(3)
  5. )
  6. AS
  7. BEGIN
  8.     SELECT
  9.         A_LOT_OF_FIELDS
  10.     FROM
  11.         MY_TABLES
  12.     WHERE
  13.         A_LOT_OF_CONDITIONS AND
  14.         CASE
  15.             WHEN @type = 'foo' THEN [condition1]
  16.             ELSE [condition2]
  17.         END
  18. END
  19. GO
  20.  
But I'm afraid I'd have to settle for either two separate procs or else something like:
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE get_fubar
  2. (
  3.     @mystring VARCHAR(20),
  4.     @type CHAR(3)
  5. )
  6. AS
  7. BEGIN
  8.     IF @type = 'foo'
  9.     BEGIN
  10.         SELECT
  11.             A_LOT_OF_FIELDS
  12.         FROM
  13.             MY_TABLES
  14.         WHERE
  15.             A_LOT_OF_CONDITIONS AND
  16.             [condition1]
  17.     END
  18.     ELSE
  19.     BEGIN
  20.         SELECT
  21.             SOME_FIELDS,
  22.             FOO,
  23.             BAR,
  24.             MORE_FIELDS
  25.         FROM
  26.             MY_TABLES
  27.         WHERE
  28.             A_LOT_OF_CONDITIONS AND
  29.             [condition2]
  30.     END
  31. END
  32. GO
  33.  
which is not much more maintainable than two procs.

Is there some way to do this? Or would it screw up the server's execution plan so much that it wouldn't be worth it even if it could be done?

Thanks for any insights,
Paul
Sep 15 '09 #1
2 1634
ck9663
2,878 Expert 2GB
try something like this....
Expand|Select|Wrap|Line Numbers
  1.  
  2. WHERE (@type = 'foo' and zipcode = '94105')  or
  3.  (@type = 'not food' and statecode = 'NY')
  4.  
  5.  
the two condition can never be both true.

Good Luck!!!

--- CK
Sep 16 '09 #2
prn
254 Expert 100+
D'oh!

Thanks, CK! I guess that makes too much sense for me to have thought of it. My logic teachers might be tempted to go back and retroactively flunk me. :)

Thanks,
Paul
Sep 16 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: FHuang | last post by:
Ok, I'm having some trouble with conditionals, for some reason PHP is screwing them up. First off, here is the code: <?php $username = "fred"; $userdata = "./$username.txt"; $fp =...
15
by: Joshua Ginsberg | last post by:
Is there any plan to include inline conditionals in Python? For example: def isNegative(x): return x < 0 ? True : False Thanks! -jag --
1
by: Paul Dale | last post by:
Hi All, I know that several of you will probably want to reply "you should write a parser", and I may. For that matter any tips on theory in that direction would be appreciated. However, if...
3
by: steven | last post by:
Is it possible to combine conditionals to call out data? I have a list of records that include a date. I can call out all dates after the date after tomorrow: IIf(Date()+1)<,...,... And I can...
4
by: Leon Lambert | last post by:
I would appreciate it if someone could help me understand NaN handling with respect to conditionals in IL code. I am playing with a small IL interpreter and having a little problem with it....
11
by: .Net Sports | last post by:
I need to convert some C# ?Conditionals over to vb.net (most likely in vb.net using If..Then statements), but the C#2VBNETconverter by KamalPatel isn't converting them: string PurchaseType =...
21
by: Frederick Gotham | last post by:
If we have the following expression: 2 < 1 What is the type of the expression, "2 < 1", or any other expression in C++ which involves comparison or inversion? More examples: 4 == 2 !3
5
by: Hul Tytus | last post by:
comp.lang.c c programs & shell conditionals How is a unix shell script made to respond to the value returned by a program compiled from c code? The shell script below is my current effort,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.