楼主就是想要一个查询器,是不:这是俺编的,你参照一下,可能有些哆嗦:-----------------------------------------------------create procedure sp_goodsinfo
(@id0 varchar(12)=null,
 @name0 varchar(15)=null,
 @corptype varchar(15)=null,
 @sort     varchar(12)=null
)
as
declare @flag int,@sql nvarchar(150)
set @flag=0
set @sql='select * from goods_info'
begin
    if (@id0 is not null)
         begin
           set @sql=@sql+' where goodsid like ''%'+@id0+'%'''
           set @flag=@flag+1
         end
    if (@name0 is not null)
          begin
               if (@flag=0)
                   set @sql=@sql+' where goodsname like ''%'+@name0+'%'''
                else
                   set @sql=@sql+' and goodsname like ''%'+@name0+'%'''
             set @flag=@flag+1
           end
     if (@corptype is not null)
           begin
                if (@flag=0)
                   set @sql=@sql+' where proid like ''%'+@corptype+'%'''
                else
                   set @sql=@sql+' and proid like ''%'+@corptype+'%'''
              set @flag=@flag+1
            end
     if (@sort is not null)
            begin
               if (@flag=0)
                   set @sql=@sql+' where sort='''+@sort+''''
                else
                   set @sql=@sql+' and sort='''+@sort+''''
             set @flag=@flag+1
             end
      
exec(@sql)
return
end

解决方案 »

  1.   

    这样的,是不:
    create procedure test( @a varchar(12),@b varchar(12),@c varchar(12),@d varchar(12))
    as
    begin
    declare @sql varchar(3000)
    set @sql=''
    if @a<>"" or @b<>"" or @c<>""
     begin
      set @sql='select * from tb where a='''+@a+''' and b='''+@b+''' and d='''+@d+''''
      exec(@sql)
      end
    return
    end
      

  2.   

    先谢谢楼上的,可能还是没理解我,郁闷那~~~~如果@a="" @b="2" @c="3"
    那@sql=select * from tb where a="" and b="2" and c="3"但数据表a字段没有""数据,就会出现一条数据也找不到我看了你的第一种方法,不知道参数怎么传过来,你一开始就设为null了解决加分,再次谢谢
      

  3.   

    select * 
    from tb
    where
    case when isnull(@a,'') = ''
                    then ''
               else a 
           end  like isnull(@a,'') 
    and
    case when isnull(@b,'') = ''
                    then ''
               else b 
           end  like isnull(@b,'')
    and
    case when isnull(@c,'') = ''
                    then ''
               else c 
           end  like isnull(@c,'')
    and
    case when isnull(@d,'') = ''
                    then ''
               else d 
           end  like isnull(@d,'')
      

  4.   

    where (a="" )or a=@a
    ...
      

  5.   

    第一种方法:一开始设为null,是为了避免前台(如delphi)没有输入参数值,所设的默认值.第一种方法是这样的: @flag是个标记,当判断参数不为空时,@flag就加1,然后再判断第二个参数,如果还不为空,并且@flag<>0那么就加二个条件..。。//如果@a="" @b="2" @c="3"
    那@sql=select * from tb where a="" and b="2" and c="3"数据表a字段没有""数据,楼主你仔细看一看 @sql这一句(where  a="" and b="2" and c="3")
    你就设置条件a="",可是你a字段没有""数据,当然一条数据也没有了..
      

  6.   

    agree lmj2003(雁不归) select * 
    from tb
    where
    (a = @a or @a = '' or @a is null)
    and
    (b = @b or @b = '' or @b is null)
      

  7.   

    谢谢,用的是第一种方法先成功了一半然后发现char和varchar有很大的区别改了一下,成功!!!!!!!!