In my code, I have a CASE WHEN
statement that includes 25 when
s, 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:
But it turns out to be:
ELSE NULL
in aCASE
expression; it will returnNULL
already if no conditions are met.CASE
expression will never be returned; unlessMSK500.S5MV16
orMSK500.S5MV17
can have a value ofNULL
?