搜一下不定参数存储过程还是要在存储过程里判断吧这是一个例子 create table test (a int, b varchar(10), c varchar(20), d int)insert test select 1,'a','b',2 union select 3,'c','d',4 gocreate proc VarProc (@b varchar(10)=null, @c varchar(20)=null, @d int=null) as declare @sql varchar(100) select @sql='select * from test where 1=1 ' +case when @b is null then '' else 'and b like '''+@b+'''' end +case when @c is null then '' else 'and c='''+@c+'''' end +case when @d is null then '' else 'and d='+rtrim(@d) end print @sql exec(@sql)goexec VarProc 'a','b' drop table test drop proc VarProc
create table test
(a int,
b varchar(10),
c varchar(20),
d int)insert test
select 1,'a','b',2 union
select 3,'c','d',4
gocreate proc VarProc
(@b varchar(10)=null,
@c varchar(20)=null,
@d int=null)
as
declare @sql varchar(100)
select @sql='select * from test where 1=1 '
+case when @b is null then '' else 'and b like '''+@b+'''' end
+case when @c is null then '' else 'and c='''+@c+'''' end
+case when @d is null then '' else 'and d='+rtrim(@d) end
print @sql
exec(@sql)goexec VarProc 'a','b'
drop table test
drop proc VarProc