[SQL-码农]字符组合式的T-SQL

来源:互联网 发布:从零开始学java mp4 编辑:程序博客网 时间:2024/06/05 14:12

这是公司的一个办法,可以判断参数Null值,无视这个Where控制

但现在还有更好的:http://blog.csdn.net/cracklibby/article/details/7562587

exec PROCEDURE @year,@month,@areaGOCREATE procedure ...@year int,@month int,@area varchar(10)ASBegindeclare @SQLbase varchar(2000)declare @SQLLink varchar(2000)declare @SQLWhere varchar(2000)set @SQLbase=' select [...],[...],[...],[...],[...]from [Tab..]'set @SQLLink=' 'set @SQLWhere='where 1=1  'if(@year is not null and @year <> ''  and @year <> ' ' )beginset @SQLWhere=@SQLWhere+'  and  year([date...]) = '''+@year+'''  'endif(@month is not null and @month <> ''  and @month <> ' ' )beginset @SQLWhere=@SQLWhere+'  and  month([date...]) = '''+@month+'''  'endif(@area is not null and @area <> ''  and @area <> ' ' )beginset @SQLWhere=@SQLWhere+'  in  '''+@area+'''  'end--select @SQLbase--select @SQLLink--select @SQLWhereexec (@SQLbase+@SQLLink+@SQLWhere)END