和在程序判断的方法差不多
if @dt2 is null
begin
se lect * from xxx where 项目 = @item and 日期 >= @dt1
end
else
begin
if @dt1 is null
begin
se lect * from xxx where 项目 = @item and 日期 <= @dt2
end
else
begin
se lect * from xxx where 项目 = @item and 日期 >= @dt1 and 日期 <= @dt2
end
end
另外一种是动态执行sql,https://www.baidu.com/baidu?word=exec+sp_executesql&ie=utf-8&tn=myie2dg&ch=6