0

In my code, I have a CASE WHEN statement that includes 25 whens, now I need to use this code in linked server, so I was forced to change the CASE WHEN statement with COALESCE, but I think I did not do it right.

Can you help me to check out, where is the problem?

Before:

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

Which generates the following error: Msg 125, Level 15, State 4, Line 1 Case expressions may only be nested to level 10

My rewrite with 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
)

Problem:

With the second expression, I can only get the result of the first condition:

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

Some pictures may be more clear,

It should be like this:

enter image description here

But it turns out to be:

enter image description here

6
  • You forgot to include said error. :)
    – Thom A
    Commented Oct 2, 2019 at 7:49
  • Also, there's no reason to have an ELSE NULL in a CASE expression; it will return NULL already if no conditions are met.
    – Thom A
    Commented Oct 2, 2019 at 7:50
  • Also, finally, in your second statement, the final CASE expression will never be returned; unless MSK500.S5MV16 or MSK500.S5MV17 can have a value of NULL?
    – Thom A
    Commented Oct 2, 2019 at 7:59
  • @Larnu Yes, actually in the second statement, it only returns the very first case: MGC100.GNTIND
    – Chen J
    Commented Oct 2, 2019 at 8:03
  • @Laurn Thank you very much for helping me. I may see my stupid problem...
    – Chen J
    Commented Oct 2, 2019 at 8:04

3 Answers 3

0

The logic in those two CASE statements are different.

In the first example when MRE100.RECTYQ = 'T' then you are executing a second CASE statement on the value of MGC100.GNTIND.

In the second example you are breaking out to the value MGC100.GNTIND as soon as the condition MRE100.RECTYQ = 'T' is met.

1
  • Thanks very much. But now I need to rewrite the first with COALESCE, because of using the Linked server. I need to avoid the error : Msg 125, Level 15, State 4, Line 1 Case expressions may only be nested to level 10.
    – Chen J
    Commented Oct 2, 2019 at 8:27
0

You primary problem is that the second CASE expression ends with an ELSE condition that yieds 0 if all other conditions where not met. As a consequence, COALESCE never goes to the third expression.

Bottom line, I think that you are overcomplicating things. CASE stops evaluating conditions as soon as a matching condition is met. As far as concerns you don't need COALESCE, and you code could be expressed as a single CASE expression, like:

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
  • Thanks very much. But now I need to rewrite the first with COALESCE, because of using the Linked server. I need to avoid the error : Msg 125, Level 15, State 4, Line 1 Case expressions may only be nested to level 10.
    – Chen J
    Commented Oct 2, 2019 at 8:28
0

The second code snippet doesn't appear to be a re-write of the first one, unless I'm mistaken.

In the first one, you are using MRE100.RECTYQ in the first CASE expression, and then the variable MGC100.GNTIND in the second CASE expression.

Case MGC100.GNTIND
    when 01 then MSK500.S5MV01

However, in the second CASE expression you are checking MRE100.RECTYQ all the time.

Case when MRE100.RECTYQ = 'T' then MGC100.GNTIND 
     when MRE100.RECTYQ = '01' then MSK500.S5MV01
1
  • Thanks very much. But now I need to rewrite the first with COALESCE, because of using the Linked server. I need to avoid the error : Msg 125, Level 15, State 4, Line 1 Case expressions may only be nested to level 10.
    – Chen J
    Commented Oct 2, 2019 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.