想先按全部查询,然后再按年查,在按行政区查,在按类型查,
当查年时,不查行政区和类型
当查行政区时,不查按年和类型,
当按类型查时,不查年和行政区,
我这么写只能按全部查询出来数了
CREATE PROCEDURE P @tmStart nvarchar(32)=null,@tmEnd nvarchar(32)=null,@xzq nvarchar(32)=null,@xmlb nvarchar(32)=null
AS
begin
if (@tmStart = null or @tmEnd = null or @xzq = null or @xmlb = null)
-- IF @tmStart is not null or @tmEnd is not null or @xzq is not null or @xmlb is not null
begin
select * from XX 
end
else if (@tmstart is not null or @tmEnd is not null and @xzq = null and @xmlb = null)
begin
print '按年过滤'
select * from XX where @tmStart<=年 and @tmEnd >= 年
end
else if ((@tmStart = null or @tmEnd = null) and @xzq is not null or @xmlb = null)
begin
print '按行政区过滤'
select * from XX where 行政区划 = @xzq
end
else if ((@tmStart = null or @tmEnd = null or @xzq = null) and @xmlb is not  null)
begin 
print '按类型过滤'
select * from XX where 类型=@xmlb
end
--IF @tmStart is not null or @tmEnd is not null or @xzq is not null or @xmlb is not null
begin
print '按全部条件过滤'
select * from XX where @tmStart<=年 and @tmEnd >= 年 and 行政区划=@xzq and 类型=@xmlb
endEND
GO