问题描述
我希望我可以在 SQl Server 2005(我知道它无效)中为我的 where 子句执行以下操作.有时@teamID(传递到存储过程中)将是现有 teamID 的值,否则它将始终为零,我想要 Team 表中的所有行.
I'm wishing I could do something like the following in SQl Server 2005 (which I know isnt valid) for my where clause. Sometimes @teamID (passed into a stored procedure) will be the value of an existing teamID, otherwise it will always be zero and I want all rows from the Team table.
我研究过使用 Case 并且操作符需要在整个语句之前或之后出现,这会阻止我根据 @teamid 的值使用不同的操作符.除了复制我的选择语句之外的任何建议.
I researched using Case and the operator needs to come before or after the entire statement which prevents me from having a different operator based on the value of @teamid. Any suggestions other than duplicating my select statements.
declare @teamid int
set @teamid = 0
Select Team.teamID From Team
case @teamid
when 0 then
WHERE Team.teamID > 0
else
WHERE Team.teamID = @teamid
end
推荐答案
你可以在没有案例的情况下做到这一点:
You can do that without a case:
SELECT Team.teamID
FROM Team
WHERE (@teamid = 0 AND Team.teamID > 0)
OR (@teamid <> 0 AND Team.teamID = @teamid)
这篇关于SQL Where 子句中的条件运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!