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'
(@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'
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'
selec * from 客户表 where 客户编号 like @id
and 客户姓名 like @name
and 客户地址 like @add
and 客户类型 like @type
客户没填的就用%阿
feelingrun(神兵天将) 不全面阿
(@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'
也许要求是:
客户地址 = '北京' and 客户类型 = 'A类'
isnull(,列)不是效率太差了!
and 客户姓名 = @name
and 客户地址 = @add
and 客户类型 = @type有四个条件,有可能得到的参数是1到4个,但前提肯定是ABD的关系。
and 客户姓名 = @name
and 客户地址 = @add
and 客户类型 = @type有四个条件,有可能得到的参数是1到4个,但前提肯定是AND的关系。
(@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)
只用一次就够了。不影响效率的。