存储过程:select account,type from t1
where type=@type
前台.net web应用程序
下拉列表中选项:全部、type1、type2、type3当选择type1、type2、type3时可以顺利将选择的数据传到存储过程中过滤数据。
但如果选择全部,存储过程如果写才能查出所有类型(type1、type2、type3
)的信息

解决方案 »

  1.   

    if @type='全部'
    begin
     select account,type from t1
    end
    esle
    begin
     select account,type from t1
     where type=@type
    end
      

  2.   

    if(@type='全部')
    begin 
         select account,type from t1
         where type in(type1、type2、type3)
    end
    else
    begin 
         select account,type from t1
         where type=@type
    end
      

  3.   

    这样是可以解决问题的,但如果存储过程是这样的:
    select account,type from t1
    where type=@type and aa=@aa and bb=@bb and cc=@cc那么通过IF的方式判断那就太繁琐了。还有更好的方法吗,谢谢!!!!!
      

  4.   

    可以这样:
    Declare @sql varchar(500)set @sql = 'select account,type from t1 where 1=1 '+ case when @type = '全部' then '' else ' and type = ' + @type end + ' and aa= '+ @aa +' and bb= '+ @bb + ' and cc= '+ @cc
    exec(@sql)