SQL中的多条件查询,该怎么解决

SQL中的多条件查询
本帖最后由 yukunjiename 于 2014-05-16 16:32:41 编辑
sql中的多条件查询,怎么做到当有参数为空值的时候,不放入where条件中

像这种

if(param1!=null)
{
    strWhere+="param1=@param1";
}



对了,是在SQL里面怎么实现?
------解决方案--------------------
引用:
我是要知道我怎么获取这个exec(@tsql)的结果是否存在


create proc [SpName]
(@p1 varchar(10),
 @p2 varchar(10),
 @p3 varchar(10),
 @p4 varchar(10))
as
begin
 declare @tsql varchar(6000)
 
 create table #t([字段列表])
 
 select @tsql='insert into #t '
             +' select * from [表名] '
             +' where 1=1 '
             +case when @p1 is not null then ' and [字段1]='''+@p1+''' ' else '' end
             +case when @p2 is not null then ' and [字段2]='''+@p2+''' ' else '' end
             +case when @p3 is not null then ' and [字段3]='''+@p3+''' ' else '' end
             +case when @p4 is not null then ' and [字段4]='''+@p4+''' ' else '' end
              
 exec(@tsql)
 
 if exists(select 1 from #t)
 begin
  [存在]
 end
 else
 begin
  [不存在]
 end
               
end