create procedure Sample
    (@id numeric=null
     @name varchar(20)=null
     @type varchar(20)=null
     @add  varchar(50))=null
as
selec * from 客户表 where 客户编号 = isnull(@id,客户编号)
                      and 客户姓名 = isnull(@name,客户姓名)
                      and 客户地址 = isnull(@add,客户地址)
                      and 客户类型 = isnull(@type,客户类型)使用:
(如只有ID):
exec Sample @id='123123'

解决方案 »

  1.   

    或者:
    create procedure Sample
        (@id numeric=null
         @name varchar(20)=null
         @type varchar(20)=null
         @add  varchar(50))=null
    as
    selec * from 客户表 where 客户编号 = (case when @id='' or @id is null then 客户编号 else @id end)
      .............  (下同)      
                
    使用:
    (如只有ID):
    exec Sample @id='123123'
      

  2.   

    很简单,用like
    selec * from 客户表 where 客户编号 like @id
                          and 客户姓名 like @name
                          and 客户地址 like @add
                          and 客户类型 like @type
    客户没填的就用%阿
      

  3.   

    同意j9988(j9988) 的
    feelingrun(神兵天将) 不全面阿
      

  4.   

    哈哈。。create procedure Sample
        (@id numeric=null
         @name varchar(20)=null
         @type varchar(20)=null
         @add  varchar(50))=null
    as
    selec * from 客户表 where 客户编号 = @id
                          or 客户姓名 = @name
                          or 客户地址 = @add
                          or 客户类型 = @type使用:
    (如只有ID):
    exec Sample @id='123123'
      

  5.   

    to:大力,OR不行的。用户选择条件可能有多个。
    也许要求是:
       客户地址 = '北京' and 客户类型 = 'A类'
      

  6.   

    哈哈那
    isnull(,列)不是效率太差了!
      

  7.   

    where 客户编号 = @id
      and 客户姓名 = @name 
      and 客户地址 = @add
      and 客户类型 = @type有四个条件,有可能得到的参数是1到4个,但前提肯定是ABD的关系。
      

  8.   

    where 客户编号 = @id
      and 客户姓名 = @name 
      and 客户地址 = @add
      and 客户类型 = @type有四个条件,有可能得到的参数是1到4个,但前提肯定是AND的关系。
      

  9.   

    create procedure Sample
        (@id numeric,
         @name varchar(20),
         @type varchar(20),
         @add  varchar(50))
    as
    declare @SQLStr varchar(8000)select @SQLStr=case when @id is not null then 'and 客户编号='+cast(@id as varchar) else '  ' end+
    case when @name is not null then 'and  客户姓名='''+@name+'''' else '  ' end+
    case when @add is not null then 'and  客户地址='''+@add+'''' else '  ' end+
    case when @type is not null then 'and  客户类型='''+@type+'''' else '  ' end,@SQLStr=right(@SQLStr,len(@SQLStr)-4),@SQLStr=case when len(ltrim(@SQLStr))>0 then 'selec * from 客户表 where '+@SQLStr else 'selec * from 客户表' end
    exec(@SQLStr)
      

  10.   

    要明白isnull是对变量的判断,不是对列值的判断。
    只用一次就够了。不影响效率的。