Nate Pet
Nate Pet

Reputation: 46322

sql t-sql else if not working properly

I have the following short version of a tsql if else if..

    IF @var = 1 
    BEGIN
    ... 

    END
    ELSE IF @var = 2
    BEGIN
    ....

    END
    ELSE IF @var = 3
    BEGIN
      ....
    END
    ....

I get a message saying Incorrect syntax near the keyword 'BEGIN' when I use it for the @var = 2 .

not sure if I am doing something wrong

Upvotes: 0

Views: 1061

Answers (1)

t-clausen.dk
t-clausen.dk

Reputation: 44356

Works just fine, you can't have nothing between begin and end:

declare @var int = 2
IF @var = 1      
BEGIN     
select 1
END     
ELSE IF @var = 2     
BEGIN     
select 2
END     
ELSE IF @var = 3     
BEGIN     
select 3
END 

Result:

2

Upvotes: 1

Related Questions