我找到了一堆类似的questions,但我无法得到确切的答案!
我在SQL where子句
中有以下条件declare @status int, @applyUserFilter bit, @user nvarchar(156)
--following are values being set by UI
set @activeStatus = 1
set @applyUserFilter = 1
set @user = 'xyzUser'
--I want to filter the list by users only when @applyUserFilter flag is true otherwise it should return the list for all users
--This is what i tried but gives syntax error
WHERE ActiveStatus = @activeStatus
AND CASE WHEN @applyUserFilter = 1 THEN User = @user
注意:这只显示where子句代码!
答案 0 :(得分:0)
怎么样:
WHERE activeStatus=@activeStatus
AND (user IN (SELECT user from #users) AND @applyUserFilter=0
OR user=@user)