ALTER PROCEDURE GetVendor_RMA_CreditMemo
(@HasCreditMemoNo INT)
BEGIN
SELECT
*
FROM
(SELECT
CreditMemoNumber,
CASE WHEN CreditMemoNumber != ''
THEN 1
ELSE 0
END AS HasCreditMemoNo
FROM
XYZ) as C
WHERE
(C.HasCreditMemoNo = @HasCreditMemoNo OR @HasCreditMemoNo = -1)
END
CreditMemoNumber
是varchar
专栏
我想实现这个目标:
CASE
WHEN @HasCreditMemoNo = 0
THEN -- select all rows with no value in CreditMemoNumber Column,
WHEN @HasCreditMemoNo = 1
THEN -- all rows that has some data,
WHEN @HasCreditMemoNo = -1
THEN -- everything regardless..
你不能用CASE来做这件事。
正确的方法是使用OR:
WHERE (@HasCreditMemoNo = 0 AND {no value in CreditMemoNumber Column})
OR
(@HasCreditMemoNo = 1 AND {all rows that has some data})
OR
(@HasCreditMemoNo = -1)
这对你有用吗?我不确定它是否会改善你的表现。你可能最好写一个if else if else
语句和三个单独的select语句,在CreditMemoNumber列上有一个索引。
ALTER PROCEDURE GetVendor_RMA_CreditMemo(@HasCreditMemoNo int)
BEGIN
select
CreditMemoNumber,
case when CreditMemoNumber != '' then 1 else 0 end as HasCreditMemoNo
from XYZ
where
(@HasCreditMemoNo = 0 and (CreditMemoNumber is null or CreditMemoNumber = ''))
or (@HasCreditMemoNo = 1 and CreditMemoNumber != '')
or (@HasCreditMemoNo = -1)
END