我的存储过程中有这一行。
CreatedBy = CASE
WHEN @Userregisteredby = 0 THEN 0
ELSE CreatedBy
END
在我的表中,CreatedBy
有0或1或任何其他值。
如果我从前端单选按钮列表发送0,我想只获得CreatedBy=0
的行。
同样,如果我从前端发送1,我想只获得CreatedBy > 0
行。对于ELSE条件,我想获得所有行。怎么做这件事?
在我的代码中,我无法使用这样的条件:
WHEN @Userregisterdby = 1 Then > 0
答案 0 :(得分:2)
我认为你需要这样的东西:
SELECT fields FROM table
WHERE
(@UserRegisteredBy = 0 and CreatedBy = 0)
or
(@UserRegisteredBy = 1 and CreatedBy > 0)
or
(@UserRegisteredBy is null)
答案 1 :(得分:1)
如果我理解你的要求,你想要
CreatedBy = 0
@Userregisteredby = 0
的所有记录拉出来
CreatedBy > 0
@Userregisteredby > 0
的所有记录拉出来
WHERE CreatedBy = CASE
-- Admin
WHEN @Userregisteredby = 1 AND CreatedBy > 0 THEN CreatedBy
-- Self
WHEN @Userregisteredby = 0 THEN 0
-- Everyone else (exclude self/admin records)
WHEN CreatedBy > 1 THEN CreatedBy
END