ID ProcessTeamID
1 1,2,
2 1,2,3,
3 1,
4 1,2,
5 1,2,
我数据库里面有一列是这样的。 存放着另一张表的ID。。 现在我想用SQL 语句 把这一列 拿出来。
ID ProcessTeamID
1 1
1 2
------解决方案--------------------
我可以说 问题没有描述清楚吗?还是我理解能力不行。
------解决方案--------------------
declare @s varchar(1000)
declare @id int
set @id=1
select @s = ProcessTeamID from tbl where ID = @id
select tbl.ID, tt.S from tbl inner join
(
select replace(reverse((left(s,charindex(',',s)))),',','') as S from(
select r,reverse(left(@s,r))+',' as s
from(
select (select count(*) from sysobjects where name<=t.name ) as r
from sysobjects t
)a where r<=len(@s)
and left(@s+',',r+1) like '%,'
)t) as tt
on ID = @id
order by tt.s
------解决方案--------------------
if object_id('[tb]') is not null drop table [tb]
go
create table tb (ID varchar(20),ProcessTeamID varchar(20))
insert into tb
select '1','1,2,' union all
select '2','1,2,3,' union all
select '3','1,' union all
select '4','1,2,' union all
select '5','1,2,'
select distinct id ,(substring(ProcessTeamID,number,charindex(',',ProcessTeamID+',',number)-number)) as ProcessTeamID
from tb, master.dbo.spt_values
where number>=1 and number<len(ProcessTeamID) and substring(','+ProcessTeamID,number,1)=','
--id ProcessTeamID
--1 1
--1 2
--2 1
--2 2
--2 3
--3 1
--4 1
--4 2
--5 1
--5 2
------解决方案--------------------
为什么4和5不拿出来啊??
------解决方案--------------------
这是一个BUG