I am trying to pass a report parameter to a stored procedure (@Time) to dictate which Category values are returned in my data set via the WHERE clause and don’t know the syntax to accomplish this correctly. I suspect a case/if will need to be used and I’ve done this before but not when needing to do a WHERE clause IN.
DECLARE @Time AS NVARCHAR
SET @Time = 'REG' --possible values of 'REG' and 'OT'
SELECT
f.[Category] AS [Category]
FROM foo f
WHERE
@Time =
CASE @Time
WHEN 'REG' THEN f.[Category] IN (A, B, C, D)
WHEN 'OT' THEN f.[Category] IN (E, F, G)
END
First, when you declare a character variable, you should always include a length. So, you are declaring @Time to be one character. I think you want 3-characters.
Second, when you declare an nvarchar() variable, assign an nvarchar() to it. The syntax is N’Reg’ rather than ‘Reg’.
Third, what is in the IN list? Do you intend for these to be constants or columns in the data?
And, finally, what you want for the WHERE clause is something like: