Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 if elses in a where clause

Author  Topic 

bill_
Starting Member

38 Posts

Posted - 2010-05-19 : 12:39:18
How do you put conditional logic in a WHERE clause of a query ia a stored procedure?

First need to make TBLX if @PARAM1 is not empty. It is a list codes from a code table.

Next need to do something like
WHERE CONDITION1 AND CONDITION2 AND
IF LEN(@PARAM1)>1
@PARAM1 IN (SELECT X FROM TBLX)

Just to start learning the very basics, I tried what's below with no luck to see if query would get no rows - got error cursor not open
WHERE COND1 AND COND2 AND
IF (0=0)
0=1

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-19 : 12:49:13
WHERE CONDITION1 AND CONDITION2 AND
( LEN(@PARAM1)<1 OR @PARAM1 IN (SELECT X FROM TBLX))

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-05-19 : 12:51:10
I think you are talking about a Catch-All query. Here is a good article about them:
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
Go to Top of Page
   

- Advertisement -