我想在where子句
中使用CASE和IN子句但是下面的查询引发了语法错误。
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
)
任何人都可以帮我解决..
谢谢, Aneesh
答案 0 :(得分:1)
尝试不使用CASE
语句,但要检查是否@UserStatus IS NULL
:
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))
)
答案 1 :(得分:0)
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
)