Reputation: 883
I want to use a CASE and IN clause inside a where clause
But the below query is throwing a syntax error.
QUERY:
SELECT *
FROM User
where isInternalUser = 0 and DeleteMark <> 1 and
User.Status IN
(
CASE @UserStatus // case for UserStatus
WHEN 0 THEN 0
WHEN 1 THEN 1
WHEN 2 THEN 2
ELSE (0,1,2)
END
)
Could anyone help me out..
Thanks, Aneesh
Upvotes: 1
Views: 599
Reputation: 102753
User.Status IN
(
SELECT s FROM (
SELECT 1 s union SELECT 2 s union SELECT 3 s
) a
WHERE @UserStatus is null or @UserStatus = s
)
Upvotes: 0
Reputation: 2782
Try without the CASE
statement, but checking whether @UserStatus IS NULL
or not:
SELECT *
FROM User
WHERE isInternalUser = 0 AND DeleteMark <> 1 AND (
(@UserStatus IS NOT NULL AND Status = @UserStatus)
OR (@UserStatus IS NULL AND Status IN (0, 1, 2))
)
Upvotes: 1