【发布时间】:2018-08-16 08:27:30
【问题描述】:
如何在where 子句中使用不同的条件列?
请参考下面的代码
if OBJECT_ID('tempdb..#MemberInfo') is not null
drop table #MemberInfo
DECLARE @GroupID varchar(60),
@Eoid varchar(60);
set @GroupID='23'
set @Eoid = null;
select a.memberid, a.membername, a.groupid, a.eoid
from membermst a
where
case
when @GroupID is not null
then a.groupid = @GroupID // this is an error
when @Eoid is not null
then a.eoid = @Eoid // this is an error
when @GroupID is null and @Eoid is null
then select all records // this is an error
end
这些是我的过滤条件
- 如果@GroupID 不为空,则查询将基于GroupID 生成
- 否则,如果@Eoid 不为空,则查询将基于Eoid 生成
- 否则,如果 GroupID 和 Eoid 均为空,则选择所有记录
注意这是我程序的条件
- groupid 和 eoid 可以同时为空
- groupid 或 eoid 都只能有一个值
- 如果 groupid 有值则 eoid 为空
- 如果 eoid 有值则 groupid 为空
- groupid 不能同时有值。那么其中任何一个都只有一个值
这可以使用 SQL 查询吗?
【问题讨论】:
标签: sql sql-server sql-server-2005