我有一个名为@status的变量,该变量在此select语句之前设置:2 s: l4 D& ^/ N5 F I& T
Select+ }2 J" y4 G/ V2 A' ^
ordr_num as num,: l' q! x$ w; q, B# I% B
ordr_date as date,+ O! L# _. L6 x) P8 b) p
ordr_ship_with as shipwith $ s' c4 P9 e7 h' z/ C* nFrom # L; w" Y8 [4 Vorder! i& N, F- f9 g+ o" }9 E# G0 F
where ordr_num = @ordrNum8 e- M _2 ]& g2 B
我只想选择ordr_ship_withif列@status 'Cancelled',否则我想为shipwith选择null。我该如何完成? * F! p k6 _3 }' b ) w6 ~7 K0 T1 D3 K% x' V 解决方案:+ ~& `: ]6 W- }' \9 ?1 _8 x8 S% S
' {+ `4 v" L3 o% \" Z& Y0 k2 n7 E
' ?9 D1 G Q* ?5 Z# ]
* E2 k7 h* k6 y. E4 y- ?
SELECT ordr_num as num, ordr_date as date, 3 ^0 E5 M3 k4 w3 S
CASE WHEN @status'Cancelled' THEN ordr_ship_with ELSE NULL END as shipwith ! o6 J6 M* t, m- L* s& K3 [
FROM order / t. q, a2 }7 N5 a% F9 J
WHERE ordr_num = @ordrNum