用COALESCE重写CASE WHEN语句后出错

时间:2019-10-02 07:44:57

标签: sql sql-server tsql

在我的代码中,我有一个CASE WHEN语句,其中包含25个when,现在我需要在链接服务器中使用此代码,因此我被迫更改了CASE WHEN语句COALESCE,但我认为我做得不好。

您能帮我看看问题在哪里吗

之前:

Case MRE100.RECTYQ
    When 'T' then
      Case MGC100.GNTIND
        when 01 then MSK500.S5MV01
        when 02 then MSK500.S5MV02
        when 03 then MSK500.S5MV03
        when 04 then MSK500.S5MV04
        when 05 then MSK500.S5MV05
        when 06 then MSK500.S5MV06
        when 07 then MSK500.S5MV07
        when 08 then MSK500.S5MV08
        when 09 then MSK500.S5MV09
        when 10 then MSK500.S5MV10
        when 11 then MSK500.S5MV11
        when 12 then MSK500.S5MV12
        when 13 then MSK500.S5MV13
        when 14 then MSK500.S5MV14
        when 15 then MSK500.S5MV15
        when 16 then MSK500.S5MV16
        when 17 then MSK500.S5MV17
        when 18 then MSK500.S5MV18
        when 19 then MSK500.S5MV19
        when 20 then MSK500.S5MV20
        when 21 then MSK500.S5MV21
        when 22 then MSK500.S5MV22
        when 23 then MSK500.S5MV23
        when 24 then MSK500.S5MV24
        when 25 then MSK500.S5MV25              
        else 0
      end
    when 'U' then MSK500.S5MVTT
    else 0
end

哪个会产生以下错误: Msg 125, Level 15, State 4, Line 1 Case expressions may only be nested to level 10

我用COALESCE重写:

COALESCE(
    Case when MRE100.RECTYQ = 'T' then MGC100.GNTIND 
         when MRE100.RECTYQ = '01' then MSK500.S5MV01
         when MRE100.RECTYQ = '02' then MSK500.S5MV02
         when MRE100.RECTYQ = '03' then MSK500.S5MV03
         when MRE100.RECTYQ = '04' then MSK500.S5MV04
         when MRE100.RECTYQ = '05' then MSK500.S5MV05
         when MRE100.RECTYQ = '06' then MSK500.S5MV06
         when MRE100.RECTYQ = '07' then MSK500.S5MV07
         when MRE100.RECTYQ = '08' then MSK500.S5MV08
         ELSE NULL
    END,
    Case when MRE100.RECTYQ = '09' then MSK500.S5MV09
         when MRE100.RECTYQ = '10' then MSK500.S5MV10
         when MRE100.RECTYQ = '11' then MSK500.S5MV11
         when MRE100.RECTYQ = '12' then MSK500.S5MV12
         when MRE100.RECTYQ = '13' then MSK500.S5MV13
         when MRE100.RECTYQ = '14' then MSK500.S5MV14
         when MRE100.RECTYQ = '15' then MSK500.S5MV15
         ELSE NULL
    END,
    Case when MRE100.RECTYQ = '16' then MSK500.S5MV16
         when MRE100.RECTYQ = '17' then MSK500.S5MV17 
         else 0
    end,
    case when MRE100.RECTYQ ='U' then MSK500.S5MVTT
         else 0
    end
)

问题:

使用第二个表达式,我只能得到第一个条件的结果:

when MRE100.RECTYQ = 'T' then MGC100.GNTIND

某些图片可能更清晰

应该是这样的:

enter image description here

但事实证明是这样的:

enter image description here

3 个答案:

答案 0 :(得分:0)

您的主要问题是,第二个CASE表达式以一个ELSE条件结束,如果不满足所有其他条件,该表达式会产生0。因此,COALESCE永远不会转到第三个表达式。

最重要的是,我认为您过于复杂了。 CASE一旦满足匹配条件就停止评估条件。就您而言,您不需要COALESCE,并且您的代码可以表示为单个CASE表达式,例如:

CASE MRE100.RECTYQ
    WHEN 'T'  THEN MGC100.GNTIND 
    WHEN '01' THEN MSK500.S5MV01
    WHEN '02' THEN MSK500.S5MV02
    WHEN '03' THEN MSK500.S5MV03
    WHEN '04' THEN MSK500.S5MV04
    WHEN '05' THEN MSK500.S5MV05
    WHEN '06' THEN MSK500.S5MV06
    WHEN '07' THEN MSK500.S5MV07
    WHEN '08' THEN MSK500.S5MV08
    WHEN '09' THEN MSK500.S5MV09
    WHEN '10' THEN MSK500.S5MV10
    WHEN '11' THEN MSK500.S5MV11
    WHEN '12' THEN MSK500.S5MV12
    WHEN '13' THEN MSK500.S5MV13
    WHEN '14' THEN MSK500.S5MV14
    WHEN '15' THEN MSK500.S5MV15
    WHEN '16' THEN MSK500.S5MV16
    WHEN '17' THEN MSK500.S5MV17 
    WHEN 'U'  THEN MSK500.S5MVTT
    ELSE 0
END

答案 1 :(得分:0)

第二个代码段似乎不是第一个的重写,除非我弄错了。

在第一个中,您在第一个CASE表达式中使用MRE100.RECTYQ,然后在第二个CASE表达式中使用变量MGC100.GNTIND

Case MGC100.GNTIND
    when 01 then MSK500.S5MV01

但是,在第二个CASE表达式中,您一直都在检查MRE100.RECTYQ

Case when MRE100.RECTYQ = 'T' then MGC100.GNTIND 
     when MRE100.RECTYQ = '01' then MSK500.S5MV01

答案 2 :(得分:0)

这两个CASE语句中的逻辑不同。

在第一个示例中,当MRE100.RECTYQ = 'T'时,您正在对CASE的值执行第二条MGC100.GNTIND语句。

在第二个示例中,只要满足条件MGC100.GNTIND,您就可以细分为值MRE100.RECTYQ = 'T'