if object_id('ta') is not null
drop table ta
create table ta (ID int,col_1 int,col_2 int)
insert into ta
select 1,30,null union all
select 2,20,null union all
select 3,40,null
if object_id('tb') is not null
drop table tb
create table tb (s_ID varchar(10),col_1 int)
insert into tb
select '1',100 union all
select '2,3',50 union all
select '8',300 union all
select '9',400
update a
set col_2 = case when (select sum(col_1) from ta where id <= a.id and
charindex(','+ltrim(id)+',',+','+(select s_id from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0)+',')> 0)
< (select col_1 from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0)
then col_1
else (select col_1 from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0) -
(select sum(col_1) from ta where id <= a.id and
charindex(','+ltrim(id)+',',+','+(select s_id from tb where charindex(','+ltrim(a.id)+',',','+s_id+',') > 0)+',')> 0)+col_1
end
from ta a
select * from ta
/*
ID col_1 col_2
----------- ----------- --------------
1 30 30
2 20 20
3 40 30
(所影响的行数为 3 行)
*/