有表如下
工號 姓名 數量 基數 開始號 終止號
1 張三 5
2 李四 6
3 王五 7
比如基數是100,希望得到結果:
工號 姓名 數量 基數 開始號 終止號
1 張三 5 100 101 105
2 李四 6 105 106 111
3 王五 7 111 112 118
----------------------------------------------------------------------
declare @tb table(工號 int, 姓名 nvarchar(10), 數量 int, 基數 int,
開始號 int, 終止號 int)
insert @tb(工號, 姓名, 數量) select 1, N'張三', 5
insert @tb(工號, 姓名, 數量) select 2, N'李四', 6
insert @tb(工號, 姓名, 數量) select 3, N'王五', 7
declare @num int,@begin int,@end int
select @num=0
update @tb set @num=case when @num=0 then 100 else @end end,
@begin=@num+1,@end=@num+數量,基數=@num,開始號=@begin,終止號=@end
select * from @tb
/*
工號 姓名 數量 基數 開始號 終止號
----------- ---------- ----------- ----------- ----------- -----------
1 張三 5 100 101 105
2 李四 6 105 106 111
3 王五 7 111 112 118
(所影響的行數爲 3 行)
*/