Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

break in case statement in T-SQL

Is possible using break command in case statement in mssql?

Because the condition is verified that expression GO, instead of doing the next CASE's transition. ssip_miktar is 5, ssip_teslim_miktar is 0 and S74MIKTAR is 5

update set ssip_teslim_miktar= ssip_teslim_miktar+
CASE WHEN ssip_miktar<@S74MIKTAR AND ssip_teslim_miktar=0 THEN @S74MIKTAR-ssip_miktar
WHEN ssip_miktar<@S74MIKTAR AND ssip_teslim_miktar>0 THEN @S74MIKTAR-(ssip_miktar+ssip_teslim_miktar) 
WHEN ssip_miktar=@S74MIKTAR AND ssip_teslim_miktar=0 THEN @S74MIKTAR 
WHEN ssip_miktar=@S74MIKTAR AND ssip_teslim_miktar>0 THEN @S74MIKTAR-ssip_teslim_miktar
 WHEN ssip_miktar>@S74MIKTAR AND ssip_teslim_miktar=0 THEN @S74MIKTAR
 WHEN ssip_miktar>@S74MIKTAR AND ssip_teslim_miktar>0 THEN @S74MIKTAR  
END,
@S74MIKTAR=@S74MIKTAR-CASE 
         WHEN  ssip_miktar<@S74MIKTAR AND ssip_teslim_miktar=0 THEN @S74MIKTAR-ssip_miktar
         WHEN  ssip_miktar<@S74MIKTAR AND ssip_teslim_miktar>0 THEN @S74MIKTAR-(ssip_miktar+ssip_teslim_miktar)
         WHEN  ssip_miktar=@S74MIKTAR AND ssip_teslim_miktar=0 THEN @S74MIKTAR
         WHEN  ssip_miktar=@S74MIKTAR AND ssip_teslim_miktar>0 THEN @S74MIKTAR-ssip_teslim_miktar
         WHEN  ssip_miktar>@S74MIKTAR AND ssip_teslim_miktar=0 THEN @S74MIKTAR
         WHEN  ssip_miktar>@S74MIKTAR AND ssip_teslim_miktar>0 THEN @S74MIKTAR
                     ELSE 0
      END
where ssip_teslim_miktar<ssip_miktar
like image 606
esquare Avatar asked May 27 '11 14:05

esquare


3 Answers

You shouldn't need to use a break because SQL Case statements don't fall through.

DECLARE @x int

SET @x = 0

SELECT CASE
    WHEN @x = 0 THEN 'zero'     -- Only this line of the expression is evaluated
    WHEN @x <> 0 THEN 'not-zero'
    END
like image 62
Phil Gan Avatar answered Oct 26 '22 15:10

Phil Gan


This is transactional SQL and is not iterative. It does not require a break because only one of the when/else clauses will be evaulated.

Try the following:

declare @test int
set @test=1
select @test = @test + 
    case 
        when 1=1 then 1
        when 2=2 then 1
        when 3=3 then 1
        when 4=4 then 1
        when 5=5 then 1
        when 6=6 then 1
        when 7=7 then 1
        when 8=8 then 1
        else 1
       end
select @test

@test is only ever 2

like image 32
James Wiseman Avatar answered Oct 26 '22 14:10

James Wiseman


This is not necessary. THEN effectively acts as a return or break, so the statement is short-circuited.

like image 38
D'Arcy Rittich Avatar answered Oct 26 '22 14:10

D'Arcy Rittich