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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 if statement within a where clause ?

Author  Topic 

BitShift
Yak Posting Veteran

98 Posts

Posted - 2006-10-11 : 13:50:18
can i do something like this ?

where
table1.id = @param1 and
table2.sales > 1 if(@param2 is not null table3.acc_code = @param2)

Im writing a stored procedure, I have 2 parameters, one of which can be null. in the case of null, i just want to ignore that part of the where caluse. if its not null then use it in the where clause. i suppose i could wrap the whole select statement in an if, but id rather not.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-11 : 13:52:21
not sure what is your logic but you can try using
case when in place of the if


KH

Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-10-11 : 13:57:00
That's just a standard logical expression which will test for the param being null OR the column being equal to the param. Once you use an OR then when one of them is true the whole thing is true. So try this
where 
table1.id = @param1 and
table2.sales > 1 and (@param2 is null or table3.acc_code = @param2)

Go to Top of Page

BitShift
Yak Posting Veteran

98 Posts

Posted - 2006-10-11 : 13:59:58
[edited to add:]
thanks, that works !
Go to Top of Page
   

- Advertisement -