I have a proc that I want to add a parameter to – the parameter will be called @AmountType. I would like to then add that @AmountType parameter into my where clause so I can filter the different amount types. The tricky part is that I want the value of @AmountType to be whatever results from the case statement part of my select.
So, ONLY display AmountType1 records, or ONLY display AmountType2 records, etc. Obviously, I can’t just do where @AmountType = Amounts because Amounts isn’t a real column.
Any ideas of how to accomplish this?
This is my statement:
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
If you want to avoid duplicated code at the expence of possible performance degradation, then use a subquery:
Otherwise duplicate code:
Then again, there may be no difference in performance at all.