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 |
|
Abhilash
Starting Member
1 Post |
Posted - 2009-08-30 : 08:22:17
|
| Hello, I am new to SQLServer Programming and would like some help from you who are veterans to programming.....I would like to know how to use CASE similar to using IFs...I know that the common usage of CASE is like this...DECLARE @TestVal intSET @TestVal = 3SELECTCASE @TestValWHEN 1 THEN 'First'WHEN 2 THEN 'Second'WHEN 3 THEN 'Third'ELSE 'Other'ENDBut I want to use the Case similar to the usage of Switch Case statement in C....i.e.,Declare @Determiner intSet @Determiner = <some value>If @Determiner = 1Begin Update tbl1.... Update tbl2....EndElse If @Deteminer = 2Begin Update tbl1.... Update tbl2.... Update tbl3....End,etc...Using Select Case returns some value right?I dont want that to happen...How to avoid using Select so that i can use Case similar to if..else.. construct? Pls HelpHopefully expecting your reply at the earliestThanking YouAbhilash |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-30 : 12:17:41
|
You're right that a CASE is an expession that returns a value based on conditions. Where as an IF/ELSE IF/Else block is used as a Control of Flow technique. They are both valid sql that are simply two different t-sql tools. You can do something like this:update <tblAlias> set <tblAlias>.col1 = case when @Determiner = 1 then ... when @determiner = 2 then ... else ... endfrom ... Be One with the OptimizerTG |
 |
|
|
|
|
|