Retrieving records based on Variables. I have 3 variables, based on which ones are selected will change the criteria for meeting search criteria.
@QCIncluded
@RMIncluded
@IsoIncluded
There is also a 'root' or basic list of systems that are always returned.
Based on that, I have the criteria set for each possible combination of options.
DECLARE @qcIncluded AS BIT = 1
DECLARE @rmIncluded AS BIT = 1
DECLARE @isoIncluded AS BIT = 1
SELECT [SystemName], [SampleDateTime], [AnalysisName], [AnalysisValueText]
FROM [CustomReporting].[ValueTbl]
WHERE
[UnitNumber] = 1
AND
CASE
WHEN @qcIncluded = 0 AND @rmIncluded = 0 AND @isoIncluded = 0 THEN
[SystemTypeID] IN (1,38,40,42,43,44,45,46,48,49,51,52,53,54,55,58,60,61,62) --Root
WHEN @qcIncluded = 1 AND @rmIncluded = 0 AND @isoIncluded = 0 THEN
[SystemTypeID] IN (1,3,37,38,39,40,42,43,44,45,46,48,49,51,52,53,54,55,58,60,61,62) --Root + QC
WHEN @qcIncluded = 0 AND @rmIncluded = 1 AND @isoIncluded = 0 THEN
[SystemTypeID] IN (1,2,38,40,42,43,44,45,46,48,49,51,52,53,54,55,58,60,61,62,63) --Root + RM
WHEN @qcIncluded = 0 AND @rmIncluded = 0 AND @isoIncluded = 1 THEN
[SystemTypeID] IN (1,38,40,42,43,44,45,46,48,49,51,52,53,54,55,57,58,60,61,62) --Root + ISO
WHEN @qcIncluded = 0 AND @rmIncluded = 0 AND @isoIncluded = 0 THEN
[SystemTypeID] IN (1,2,3,37,38,39,40,42,43,44,45,46,48,49,51,52,53,54,55,58,60,61,62,63) --Root + QC + RM
WHEN @qcIncluded = 0 AND @rmIncluded = 0 AND @isoIncluded = 0 THEN
[SystemTypeID] IN (1,3,37,38,39,40,42,43,44,45,46,48,49,51,52,53,54,55,57,58,60,61,62) --Root + QC + ISO
WHEN @qcIncluded = 0 AND @rmIncluded = 0 AND @isoIncluded = 0 THEN
[SystemTypeID] IN (1,2,38,40,42,43,44,45,46,48,49,51,52,53,54,55,57,58,60,61,62,63) --Root + RM + ISO
WHEN @qcIncluded = 0 AND @rmIncluded = 0 AND @isoIncluded = 0 THEN
[SystemTypeID] IN (1,2,3,37,38,39,40,42,43,44,45,46,48,49,51,52,53,54,55,57,58,60,61,62,63) --Root + QC + RM + ISO
END
Receiving an error 'near the keyword 'IN'
How do I implement the CASE properly?