在case语句中分配多个值

时间:2014-04-08 17:44:52

标签: sql-server tsql

我想在游标中执行以下操作。 是否可以只使用一个案例来分配多个值?

CASE 
    when @region = ‘Region 1’ then @sum_region1 = @sum_region1 + @sum_actual
    when @region = ‘Region 2’ then @sum_region2 = @sum_region2 + @sum_actual    
    when @region = ‘Region 3’ then @sum_region3 = @sum_region3 + @sum_actual
    when @region = ‘Region 4’ then @sum_region4 = @sum_region4 + @sum_actual 
End 

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

    IF @region = 'Region 1'
    BEGIN
       SET @sum_region1 = @sum_region1 + @sum_actual
    END
    IF @region = 'Region 2'
    BEGIN
       SET @sum_region2 = @sum_region2 + @sum_actual
    END
    IF @region = 'Region 3'
    BEGIN
       SET @sum_region3 = @sum_region3 + @sum_actual
    END
    IF @region = 'Region 4'
    BEGIN
       SET @sum_region4 = @sum_region4 + @sum_actual
    END