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)
 Case

Author  Topic 

Exir
Posting Yak Master

151 Posts

Posted - 2009-02-09 : 06:04:17
How can i have something like this:

SELECT CASE
WHEN @a='condition1' THEN (select * from request where id=1)
WHEN @a='condition2' THEN ( select * from requestcondition where name='something')
END

I want to hav edifferent selects depends on the content of @a

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-09 : 06:10:02
you need to use IF instead

IF @a='condition1'
select * from request where id=1
IF @a='condition2'
select * from requestcondition where name='something'


Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-02-09 : 06:10:40
sub queries dont allowed in case statement
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-02-09 : 06:12:32
Try Like this

declare @a int,@str varchar(255)
select @str = ''
select @a = 1
select @str = 'select * from emptable' where @a= 1
select @str ='select * from oas_pms.dbo.tbl_pms_projects' where @a = 2

exec(@str)

Jai Krishna
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-09 : 06:15:10
why dynamic sql?
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-02-09 : 06:15:25
sub queries are allowed in case statement but need to return only one column value

Jai Krishna
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-02-09 : 09:08:32
quote:
Originally posted by Jai Krishna

sub queries are allowed in case statement but need to return only one column value

Jai Krishna



There is no sub-query in Visakh's code.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-02-09 : 09:18:14
Visakh's code is effecient to use

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-09 : 09:39:52
quote:
Originally posted by madhivanan

Visakh's code is effecient to use

Madhivanan

Failing to plan is Planning to fail


wow...finally Madhi is back!
Go to Top of Page
   

- Advertisement -