--试试这个.declare @temp varchar(30)
select @temp='3,4,5'
select * from tbl_subjects where charindex(cast(subjectID as varchar(10)),@temp)>0
select @temp='3,4,5'
select * from tbl_subjects where charindex(cast(subjectID as varchar(10)),@temp)>0
调试欢乐多
--创建表
create table tbl_subjects(subjectID int)
--插入数据
insert tbl_subjects
select 3
union select 5
union select 8
union select 4
--查询
declare @temp varchar(30)
select @temp='3,4,5'
select * from tbl_subjects where charindex(cast(subjectID as varchar(10)),@temp)>0
--结果
/*
subjectID
-----------
4
3
5(所影响的行数为 3 行)
*/
select @temp='3,4,5'
select * from tbl_subjects
where charindex(','+
cast(subjectID as varchar(10))'+',' , ','+@temp+',')>0
select @temp='3458' 时
你的结果有误
select @temp='3,48,5'
select * from tbl_subjects
where charindex(','+
cast(subjectID as varchar(10))+',' , ','+@temp+',')>0
这样就没错了
select @temp='3,4,5'
exec(select * from tbl_subjects where subjectID in '+'('+''''@temp+''''+')')