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
 SQL Server Development (2000)
 In-line if-else block?

Author  Topic 

versatilewt
Starting Member

2 Posts

Posted - 2006-04-20 : 09:20:24
Hi,

I'm writing some complex queries on a very poorly designed database. We're running on MS SQL Server2000. I was wondering if its possible to do in-line if-else blocks within a query.. i.e.


SELECT *
FROM tablename
IF (condition1 = 'a')
WHERE condition2 = 'a'
ELSE
WHERE condition2 = 'b'


Thanks,

Brian

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2006-04-20 : 09:25:07
Use a CASE statement. i.e.
WHERE condition2 = CASE WHEN condition1 = 'a' THEN 'a' ELSE 'b' END


Mark
Go to Top of Page

versatilewt
Starting Member

2 Posts

Posted - 2006-04-20 : 11:29:36
Mark,
Thanks for the reply. I think I over simplified my question a bit. Here's exactly what I'm trying to do. I want to set WHERE conditions optionally, based on a subquery.

The data isn't given explicity, so I have to kind of infer it through the query. I want to set a where condition of e.stafflevelid = m.stafflevelid and project_dept = manager_dept ONLY when then Manager level is, say 4 (which is determined from a subquery).

So, for the query


WHERE
e.active <> 0
AND
/* THIS IS THE OPTIONAL WHERE CONDITION */
/* IF THE MANAGER'S STAFF LEVEL SATISFIES SUBQUERY*/
(mgr.stafflevelid IN (/* HERE WOULD BE SUBQUERY)
/* THEN SET THESE WHERE CONDITIONS*/
e.stafflevelid=m.stafflevelid
p.dept = m.dept)


And, I want this condition to run with other WHERE statements

I know it's not pretty, but I'm working with bad structure.

Thanks,

Brian
Go to Top of Page
   

- Advertisement -