日期:2014-05-18  浏览次数:20520 次

用SQL语句怎样将用逗号的数值类型字段的值各增加100?
请教下各位
select a from b 返回的结果如下

11,12,13
12,13,14
21,22,23,24,25
39,40,41,42,43,44
11,12

我想把这里的数值都各增加100,更新后的结果如下
111,112,113
112,113,114
121,122,123,124,125

麻烦哪位帮忙写些语句,谢谢


------解决方案--------------------
SQL code

--先拆分,相加,再合并,再更新
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 行受影响)
*/