select *
from mytable
where (if @key=0
pkey>=0
else
pkey = @key)
@key
是传递给存储过程的值,pkey
是mytable
.
select *
from mytable
where (if @key=0
pkey>=0
else
pkey = @key)
@key
是传递给存储过程的值,pkey
是mytable
.
这个怎么样:
select *
from mytable
where ((@key=0 AND pkey>=0) OR (@key<>0 AND pkey = @key))
您使用CASE
(有点像您尝试的方式if
)如下。(演示)
select *
from mytable
where pkey = case when @key <> 0 then @key
else Abs(pkey) end
这对你有用。通过使用 case 语句,您可以应用动态过滤器..
select *
from mytable
where 1 = case
when @key=0 then case when pkey>=0 then 1 else 0 end
else case when pkey = @key then 1 else 0 end
end