日期:2014-05-17 浏览次数:20714 次
create table #tb(id int,DocumentID varchar(10),remarks varchar(10))
insert into #tb
select 1,'HVOU00001','啊哟'
union all select 2,'HVOU00002','啊'
union all select 3,'HVOU00003','S'
union all select 4,'HVOU00004','5'
union all select 5,'HDXA00001','D'
union all select 6,'HDXA00002','A'
union all select 7,'HDXA00003','说的'
union all select 8,'HDXA00004','DS'
union all select 9,'HDXA00005','DSA'
go
--查询输入参数
declare @DocumentID varchar(100)
set @DocumentID = 'HDXA00001,HDXA00003-HDXA00005,HVOU00002-HVOU00004'
select a.*
from #tb a
inner join
(
select case when CHARINDEX('-',[key])>0 then LEFT([key],CHARINDEX('-',[key])-1) else [key] end as fromkey
,case when CHARINDEX('-',[key])>0 then right([key],len([key])-CHARINDEX('-',[key])) else '' end tokey
from
(select SUBSTRING([key],number,CHARINDEX(',',[key]+',',number)-number) as [key]
from (select @DocumentID+',' as [key]) a, master..spt_values
where number >=1 and type='p'
and number<len([key]) and substring(','+[key],number,1)=','
)t
)b on a.DocumentID=b.fromkey or a.DocumentID between b.fromkey and b.tokey
order by id
drop table #tb
/*
id DocumentID remarks
2 HVOU00002 啊
3 HVOU00003 S
4 HVOU00004 5
5 HDXA00001 D
7 HDXA00003 说的
8 HDXA00004 DS
9 HDXA00005 DSA
*/