日期:2014-05-18 浏览次数:20588 次
--先拆分,相加,再合并,再更新 if object_id('tb') is not null drop table tb go create table tb ( col varchar(30) ) go insert into tb select '11,12,13' union all select '12,13,14' union all select '21,22,23,24,25' union all select '39,40,41,42,43,44' union all select '11,12' go with cte as ( select col,addcol=substring(col,number,charindex(',',col+',',number)-number)+100 from tb a cross join master..spt_values b where type='p' and number between 1 and len(col) and substring(','+col,number,1)=',' ) update tb set col=b.col2 from tb a inner join ( select col,col2=stuff((select ','+ltrim(addcol) from cte where col=t1.col for xml path('')),1,1,'') from cte t1 group by col ) b on a.col=b.col go select * from tb /* col ------------------------------ 111,112,113 112,113,114 121,122,123,124,125 139,140,141,142,143,144 111,112 (5 行受影响) */