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
 General SQL Server Forums
 New to SQL Server Programming
 PLS HELP TO AVOID SELECT & USE CASE SIMILAR TO IF

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 int
SET @TestVal = 3

SELECT
CASE @TestVal
WHEN 1 THEN 'First'
WHEN 2 THEN 'Second'
WHEN 3 THEN 'Third'
ELSE 'Other'
END


But I want to use the Case similar to the usage of Switch Case statement in C....

i.e.,

Declare @Determiner int
Set @Determiner = <some value>

If @Determiner = 1
Begin
Update tbl1....
Update tbl2....
End

Else If @Deteminer = 2
Begin
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 Help



Hopefully expecting your reply at the earliest

Thanking You

Abhilash

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 ...
end
from ...


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -