日期:2014-05-18 浏览次数:20450 次
create table # (col varchar(100)) insert into # select '1,102,' union all select '1,105,' union all select '2,205,' union all select '1,101,10102,1010202,' union all select '2,202,20203,' union all select '2,203,20303,' union all select '1,104,10402,' union all select '1,105,21,' union all select '2,206,50008164,50008822,' union all select '1,105,50008164,50001705,211509,' union all select '1,105,50008164,50001705,50008398,50008400,' select top 1000 id=identity(int,1,1)into #t from sysobjects,syscolumns select *from # select distinct col=substring(a.col,b.id,charindex(',',a.col+',',b.id)-b.id) from # a,#t b where substring(','+a.col,b.id,1)=',' order by col /* col ---------------------------------------------------------------- 1 101 10102 1010202 102 104 10402 105 2 202 20203 203 20303 205 206 21 211509 50001705 50008164 50008398 50008400 50008822 (23 row(s) affected) */
------解决方案--------------------
--建立环境 create table a (col1 varchar(1000)) insert into a select '1,102,' union all select '1,105,' union all select '2,205,' union all select '1,101,10102,1010202,' union all select '2,202,20203,' union all select '2,203,20303,' union all select '1,104,10402,' union all select '1,105,21,' union all select '2,206,50008164,50008822,' union all select '1,105,50008164,50001705,211509,' union all select '1,105,50008164,50001705,50008398,50008400,' ---执行語句 declare @exec varchar(8000) set @exec='' select @exec=@exec+col1 from a set @exec='select '+ left(replace(@exec,',',' union select '),len(replace(@exec,',',' union select '))-len('un