在编辑存储过程的时候,如果当某个条件为空时,就不搜索某个where子句,这在构造SQL语句很好写,但是直接写SQL语句就很麻烦了.
declare @gsid int;
declare @key varchar(200);
declare @daydate varchar(50);
begin
set @gsid = 917;
set @key = 'sh';
set @daydate = '2006-6-7';
select showname,info_id,gsid from info where 1=1
and (@gsid <> 0 and gsid=@gsid or @gsid = 0)
and showname like '%'+@key+'%'
and (dateandtime>=convert(smalldatetime,@daydate) and @daydate<>'' or @daydate='')
end
以上的SQL: 当要判断@GJ不为空时才写and country=@GJ
直接像下面写是错误的:
select * from sometbl
if @GJ<>''
AND country=@GJ
Link URL: http://blog.sina.com.cn/s/blog_5116ad5f0100abzk.html
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/586303/viewspace-530061/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/586303/viewspace-530061/