views:

46

answers:

5

I must update table, and I can't use else if in tsql. Is it exists?

Status =
(
CASE
WHEN
(Status in (0))
THEN
0
ELSE IF Status in (1)
THEN 
7
ELSE
Status
END
+1  A: 

Use again CASE WHEN:

Status = CASE WHEN Status in (0)
                THEN 0
                ELSE CASE WHEN Status in (1)
                            THEN 7
                            ELSE Status
                     END
         END

or

Status = CASE WHEN Status in (0) THEN 0
              WHEN Status in (1) THEN 7
              ELSE Status
         END
Parkyprg
+2  A: 

The CASE WHEN syntax does support multiple conditions:

Status = CASE WHEN Status = 0 THEN 0
              WHEN Status = 1 THEN 7
              ELSE Status
         END

Since you are comparing the same field for equality in both cases, you can use the shorter notation:

Status = CASE Status WHEN 0 THEN 0
                     WHEN 1 THEN 7
                     ELSE Status
         END
Heinzi
A: 

Yes, you can use IF statements in SQL. Check this

   Declare @x int
    set @x = 29
    if @x = 29 print 'The number is 29'
    if @x = 30 print 'The number is 30'
muek
Not helpful for use inside a set based update statement.
Shannon Severance
+1  A: 

You should use the following syntax:

Status =
    (CASE WHEN (Status in (0)) THEN 0
          WHEN (Status in (1)) THEN 7
          ELSE Status
     END)
Ronald Wildenberg
+1  A: 

try

 Status =
(
CASE
WHEN
(Status in (0))
THEN
0
WHEN Status in (1)
THEN 
7
ELSE
Status
END 
SQLMenace