下面的代码虽然是对varchar型进行参数查询,但同样适用于int型字段
---测试数据---
if object_id('[t1]') is not null drop table [t1]
go
create table [t1]([id] int,[text] varchar(20))
insert [t1]
select 1,'A' union all
select 2,'B' union all
select 2,'C' union all
select 3,'C' union all
select 4,'D'
---临时表---
if object_id('[temp]') is not null drop table [temp]
go
create table [temp] (field varchar(50))
---参数---
declare @parameter varchar(100)
set @parameter='A,C'
declare @pos int
declare @oldPos int
declare @tempstr varchar(100)
set @pos=1
set @oldPos=1
while @pos<len(@parameter)
begin
set @pos=charindex(',',@parameter,@oldpos)
if @pos>0
begin
set @tempstr=substring(@parameter,@oldpos,@pos-@oldpos)
set @oldpos=@pos+1
end
else
begin
set @tempstr=substring(@parameter,@oldpos,len(@parameter)-@oldpos+1)
set @pos=len(@parameter)
end
--放到临时表
insert [temp]
select @tempstr
end
---查询---
select * from t1 where [text] in (@parameter)
select * from t1 where [text] in (select field from [temp] where field is not null)
---结果---
id text
----------- --------------------
(0 行受影响)
id text
----------- --------------------
1 A
2 C
3 C
(3 行受影响)
这边咋不能贴SQL代码,⊙﹏⊙b汗~