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 2005 Forums
 Transact-SQL (2005)
 where clause in sql statement

Author  Topic 

cardgunner

326 Posts

Posted - 2010-08-09 : 13:52:53
I trying to write a where clause in my sql statement

declare @catg varchar(5)
set @catg='MT004'

select *
from tbla
where case when @catg='' then tbla.t_ctg in ('MT005', 'ME004') else tbla.t_ctg=@catg

What I'm trying to do is if there is a catg specified then run it with that catg if there is no catg specified then run it with the default catgs of MT005 and ME004.

The error I get when I run it is "incorrect syntax near the keyword in"

CardGunner

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-08-09 : 14:02:04
where

( @catg = '' and tbla.t_ctg in ('MT005', 'ME004') )

or tbla.t_ctg=@catg

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

cardgunner

326 Posts

Posted - 2010-08-09 : 14:07:54
Yep works like a charm. Thank you very much!

CardGunner
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-08-09 : 14:29:54
You're Welcome

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -