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 2008 Forums
 Transact-SQL (2008)
 Conditional where clause

Author  Topic 

LearningSQLKid
Yak Posting Veteran

51 Posts

Posted - 2012-02-06 : 16:22:18
Hi Guys

Any idea how can i use/not use where clause based on some condition

For example

If variable value=1 then use query with where clause other wise exclude where clause from the query and execute

The easy way is to repeat the same query in if else block but that causes the repetition and if the quries are big and the difference is just the where clause then its not that the good way .... is that possible to exclude where clause based on some condition
Thanks in Advance..

Select Knowledge from LearningProcess

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2012-02-06 : 16:28:42
[CODE]select <whatever>
from MyTable
where (@Var = 1 AND <Other conditions>)
OR @Var <> 1[/CODE]HTH

=================================================
Men shout to avoid listening to one another. -Miguel de Unamuno
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-06 : 16:58:46
wont this suffice?

select <whatever>
from MyTable
where <Other conditions>
OR @Var <> 1



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

Go to Top of Page
   

- Advertisement -