create table t0444(id int,col varchar(1)) insert into t0444 select 1,'a' union all select 2,'b' union all select 3,'c' union all select 4,'d' union all select 5,'e' union all select 6,'f' union all select 7,'g' union all select 8,'h' union all select 9,'i'declare @t varchar(20) set @t='4,1,6,3' exec('select * from t0444 where id in ('+@t+')') /* id col ----------- ---- 1 a 3 c 4 d 6 f */select * from t0444 where id in (4,1,6,3) order by charindex(','+cast(id as varchar(20))+',',','+'4,1,6,3'+',') /* id col ----------- ---- 4 d 1 a 6 f 3 c */
create table t0444(id int,col varchar(1))
insert into t0444
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'d' union all
select 5,'e' union all
select 6,'f' union all
select 7,'g' union all
select 8,'h' union all
select 9,'i'declare @t varchar(20)
set @t='4,1,6,3'
exec('select * from t0444 where id in ('+@t+')')
/*
id col
----------- ----
1 a
3 c
4 d
6 f
*/select * from t0444 where id in (4,1,6,3)
order by charindex(','+cast(id as varchar(20))+',',','+'4,1,6,3'+',')
/*
id col
----------- ----
4 d
1 a
6 f
3 c
*/