我有一个变量,名为@status我这个select语句之前设置:
Select
ordr_num as num,
ordr_date as date,
ordr_ship_with as shipwith
From
order
where ordr_num = @ordrNum
我只是想选择ordr_ship_with
如果列@status <> 'Cancelled'
,否则我要选择空的shipwith。 如何做到这一点?
我有一个变量,名为@status我这个select语句之前设置:
Select
ordr_num as num,
ordr_date as date,
ordr_ship_with as shipwith
From
order
where ordr_num = @ordrNum
我只是想选择ordr_ship_with
如果列@status <> 'Cancelled'
,否则我要选择空的shipwith。 如何做到这一点?
SELECT ordr_num as num, ordr_date as date,
CASE WHEN @status<>'Cancelled' THEN ordr_ship_with ELSE NULL END as shipwith
FROM order
WHERE ordr_num = @ordrNum
尝试了这一点
Select
ordr_num as num,
ordr_date as date,
CASE
WHEN @Status <> 'Cancelled' THEN ordr_ship_with
ELSE NULL END
as shipwith
From order
where ordr_num = @ordrNum
虽然我有一种感觉,你状态处于Order表的实际列。 在这种情况下,这样做:
Select
ordr_num as num,
ordr_date as date,
CASE
WHEN Status <> 'Cancelled' THEN ordr_ship_with
ELSE NULL END
as shipwith
From order
where ordr_num = @ordrNum
Select
ordr_num as num,
ordr_date as date,
CASE WHEN @status <> 'Cancelled' THEN ordr_ship_with ELSE NULL END as shipwith
From
order where ordr_num = @ordrNum
使用案例: http://msdn.microsoft.com/en-us/library/ms181765.aspx
试试这个
SELECT CASE
WHEN @status <> 'cancelled' THEN ordr_ship_with
ELSE null
END AS shipwith, ... other fields