你这么奇葩的写法真的很少见。。。
and用于布尔值判断,当and两侧都为true时,才会执行
如果你的@type=1,那么第一段
where 0=@type and a.id in(select id form ...)
明显就是 false and true或者false and false,肯定结果集=0咯
另外,你就不能先判断你的参数再来查询吗,union all效率那么低下。
if @type=0
select * from a where a.id in(select id form ...)
else if @type=1
select * from a where a.id='aaa'
再强调一次,对于参数的判断是把参数放左边,这就类似于
如果 你的性别=男的 和 如果 男的=你的性别,哪个更通顺一点!!!
你这样乱写代码真心很丑!!!!!!!!!!!!!!!!!!!!!!!!!!!!