SQL code/*
create table t1 (a varchar(10),b int,c int)
insert into t1 values ('记录1',20,0)
insert into t1 values ('记录2',20,0)
insert into t1 values ('记录3',20,0)
drop table t1*//*
CREATE FUNCTION fs (@id int)
RETURNS int AS
BEGIN
DECLARE @s int
select @s= isnull(sum(b),0) from t2 where id < @id
select @s = @s+ b from t2 where id = @id
RETURN @s
END*//*
CREATE procedure dbo.updatetable
@v int
as
select IDENTITY(INT,1,1) as id,* into t2 from t1
update t2 set c = dbo.fs(id)
update t2 set c =0 where c <= @v
update t2 set c = c - @v where b = (select min(b) from t2 where b>0)
update t2 set c =0 where c <= 0
update t2 set b = c where c <= b
select * from t2
drop table t2
[Code=SQL]
/*
create table t1 (a varchar(10),b int,c int)
insert into t1 values ('记录1',20,0)
insert into t1 values ('记录2',20,0)
insert into t1 values ('记录3',20,0)
drop table t1
*/
/*
CREATE FUNCTION fs (@id int)
RETURNS int AS
BEGIN
DECLARE @s int
select @s= isnull(sum(b),0) from t2 where id < @id
select @s = @s+ b from t2 where id = @id
RETURN @s
END
*/
/*
CREATE procedure dbo.updatetable
@v int
as
select IDENTITY(INT,1,1) as id,* into t2 from t1
update t2 set c = dbo.fs(id)
update t2 set c =0 where c <= @v
update t2 set c = c - @v where b = (select min(b) from t2 where b>0)
update t2 set c =0 where c <= 0
update t2 set b = c where c <= b
select * from t2
drop table t2