我有一个proc,我想添加一个参数 - 该参数将被称为@AmountType
。我想将@AmountType
参数添加到我的where
子句中,以便我可以过滤不同的金额类型。棘手的部分是我希望@AmountType
的值是我选择的case语句部分的结果。
因此,仅显示AmountType1记录,或仅显示AmountType2记录等。显然,我不能只where @AmountType = Amounts
Amounts
,因为ALTER PROCEDURE spProc1
@Date datetime
AS
BEGIN
Select
location,
workDate,
case
when dollarAmount1 - dollarAmount2 < 0 Then 'AmountType1'
when dollarAmount1 - dollarAmount2 > 0 Then 'AmountType2'
when dollarAmount1 - dollarAmount2 = 0 Then 'AmountType3'
End As Amounts
From
table1
Where
@Date = workDate
END
不是真正的列。
有关如何实现这一目标的任何想法?
这是我的陈述:
{{1}}
答案 0 :(得分:0)
如果您希望在可能的性能下降时避免重复代码,请使用子查询:
select
location,
workDate,
Amounts
from (
Select
location,
workDate,
case
when dollarAmount1 - dollarAmount2 < 0 Then 'AmountType1'
when dollarAmount1 - dollarAmount2 > 0 Then 'AmountType2'
when dollarAmount1 - dollarAmount2 = 0 Then 'AmountType3'
End As Amounts
From
table1
Where
@Date = workDate
) foo
where
Amounts = @AmountType
否则重复代码:
Select
location,
workDate,
case
when dollarAmount1 - dollarAmount2 < 0 Then 'AmountType1'
when dollarAmount1 - dollarAmount2 > 0 Then 'AmountType2'
when dollarAmount1 - dollarAmount2 = 0 Then 'AmountType3'
End As Amounts
From
table1
Where
@Date = workDate
and
@AmountType = case
when dollarAmount1 - dollarAmount2 < 0 Then 'AmountType1'
when dollarAmount1 - dollarAmount2 > 0 Then 'AmountType2'
when dollarAmount1 - dollarAmount2 = 0 Then 'AmountType3'
End
然后,性能可能没有差别。