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過濾