473,884 Members | 2,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conditionals in a WHERE clause?

prn
254 Recognized Expert Contributor
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 1654
ck9663
2,878 Recognized Expert Specialist
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 Recognized Expert Contributor
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
2390
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 = fopen("$userdata", "r", 1); $origpass = fread($fp, 5); if($passcount = 1) {
15
2597
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
1992
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 you would indulge me in my regex question I would also be most grateful. I'm writing an edi parser and to be in compliance with the specification
3
1443
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 call dates before next week: IIf(< Date()+7,...,... But I'd like to isolate those that fall between the day after tomorrow and next week. I was thinking that this would work, but it doesn't:
4
2026
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. Following is a small piece of C# code that gets compiled by VS2005 to simulate my problem. static void Main(string args) { Double db1 = 1.0; Double db2 = Double.NaN; if (db1 <= db2) db2 = 1.0;
11
1805
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 = (Convert.ToString(drwData) == "True") ? "ID" : "PID"; XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ? "Highlight" : "Selection");
21
1558
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
2171
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, which doesn't work. The c code shows what was used to generate a.out, seen in the shell script. The value returned was varied from 0 to 1 for testing.
0
9954
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10869
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9591
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7137
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4231
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.