I have 2 variables declared i.e @empCode
and @centerCode
. A center contains multiple employees. I have another variable that is @IsEmployeeBased
Now if @IsEmployeeBased
= 1 then it will use @empCode
else it will use @centerCode
.
My query is something like this.
DECLARE @IsEmployeeBased INT = 1;
DECLARE @empCode INT = 1;
DECLARE @centerCode INT = 3;
SELECT * FROM Employee
WHERE Department = 'Information Tech'
AND
CASE WHEN @IsEmployeeBased = 1 THEN ID = @empCode ELSE CenterCode = @centerCode
This is returning error, which I suppose is CASE is not allowed in where clause. Is there any way to get the desired query working ?
Here is sample data:
CREATE TABLE Employee
(
ID Int,
EmployeeName VARCHAR(25),
Department VARCHAR(25),
CenterCode INT
)
INSERT INTO Employee VALUES (1, 'Asim', 'Information Tech', 4)
INSERT INTO Employee VALUES (2, 'Ali', 'Information Tech', 2)
INSERT INTO Employee VALUES (3, 'Isaac', 'Information Tech', 3)
INSERT INTO Employee VALUES (4, 'Swagger', 'Information Tech', 4)
INSERT INTO Employee VALUES (5, 'Nadine', 'Information Tech', 2)
INSERT INTO Employee VALUES (6, 'Julie', 'Information Tech', 4)
INSERT INTO Employee VALUES (7, 'Meachum', 'Information Tech', 3)
INSERT INTO Employee VALUES (8, 'Bob Lee', 'Information Tech', 4)