SQL語句中另類IF--ELSE
declare @Conditions varchar(10)
set @Conditions=''
select * from TabelA
where ((@Conditions<>'' AND colName=@Conditions)
or (@Conditions='' AND colName<>@Conditions))
--改進版
select * from TabelA
where (colName=@Conditions or @Conditions='')
--查找表TabelA中記錄,如果變量@Conditions為空,則條件為colName<>@Conditions,否則根據@Conditions過濾
set @Conditions=''
select * from TabelA
where ((@Conditions<>'' AND colName=@Conditions)
or (@Conditions='' AND colName<>@Conditions))
--改進版
select * from TabelA
where (colName=@Conditions or @Conditions='')
--查找表TabelA中記錄,如果變量@Conditions為空,則條件為colName<>@Conditions,否則根據@Conditions過濾
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步