日期:2014-05-18 浏览次数:20600 次
create table tb(id int identity(1,1),sz varchar(20))
insert into tb
select '1,2,3,4,5,6'
go
;with cte as
(
select a.id,
convert(int,substring(a.sz,b.number,charindex(',',a.sz+',',b.number+1)-b.number)) sz
from tb a,master..spt_values b
where b.type = 'P' and b.number between 1 and len(a.sz)
and substring(','+a.sz,b.number,1) = ','
)
select a.id,a.sz,b.sz,c.sz,d.sz,e.sz
from cte a join cte b on a.id = b.id and a.sz < b.sz
join cte c on b.id = c.id and b.sz < c.sz
join cte d on c.id = d.id and c.sz < d.sz
join cte e on d.id = e.id and d.sz < e.sz
group by a.id,a.sz,b.sz,c.sz,d.sz,e.sz
drop table tb
/****************************
id sz sz sz sz sz
----------- ----------- ----------- ----------- ----------- -----------
1 1 2 3 4 6
1 1 2 4 5 6
1 1 2 3 4 5
1 2 3 4 5 6
1 1 2 3 5 6
1 1 3 4 5 6
(6 行受影响)