最近小弟有个问题,求sql语句
有个变量班级 ,默认值为-1 当班级=-1时,返回全部记录,如果班级=10时,只返回10班的记录。
一般来说应该这样写
declare @class varchar(20)
if @class=-1
begin
select * from xxx
end else
begin
select * from xxx where class=@class
end
但是我中间的语句有些长,想找一点简单点的写法
比如有没有可能这样的写法
select * from xxx where (if @class='-1' then 1=1 else class=@class end)
这个语法肯定不对,我是想说有没有类似这样的写法,通过where条件就能实现我的要求,谢谢
------解决方案--------------------
select * from xxx
where @class=-1 or @class=class