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.
| Author |
Topic |
|
Exir
Posting Yak Master
151 Posts |
Posted - 2010-03-09 : 05:26:45
|
| HiI want to write a query like this:if @parameter1=1 then : select * from table1 where id=@parameter2and if @parameter1=2 then : select * from table1 where name=@parameter2 andif @parameter1=3 then : select * from table1 where family=@parameter2How can i write this query using 'case' ? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-09 : 05:33:03
|
| select * from table1 where (id=@parameter2and @parameter1=1 )or(name=@parameter2 and@parameter1=2 ) or(family=@parameter2and@parameter1=3 )MadhivananFailing to plan is Planning to fail |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-09 : 05:39:05
|
| could you please try thisselect * from table1 wherecase when @parameter1=1 then id when @parameter1=2 then name when @parameter1=3 then family else null end=@parameter2 |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-09 : 05:44:43
|
quote: Originally posted by madhivanan select * from table1 where (id=@parameter2and @parameter1=1 )or(name=@parameter2 and@parameter1=2 ) or(family=@parameter2and@parameter1=3 )MadhivananFailing to plan is Planning to fail
Hi Mr.Madhi..This sounds good..could you please tell me ,wat about my query interms of performance oriented(Ref:please see my previous reply) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-09 : 05:47:24
|
| CASE expression in the WHERE clause may slow down the performance.But OP should test the solutions with large set of dataMadhivananFailing to plan is Planning to fail |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-09 : 05:52:30
|
quote: Originally posted by madhivanan CASE expression in the WHERE clause may slow down the performance.But OP should test the solutions with large set of dataMadhivananFailing to plan is Planning to fail
Thank you Mr.Madhi for your response.much Appreciated.Iam learnig from you alot here... |
 |
|
|
|
|
|