临时表#t在存储过程中某一步之后数据如下
行号 Num1 Num2
1 5
2 6
3 5
4 8
5 0希望通过update #t 使Num2的每一行都等于前面行所有Num1的和即希望得到的结果是:
行号 Num1 Num2
1 5 5
2 6 11
3 5 16
4 8 24
5 0 24求update 语句
行号 Num1 Num2
1 5
2 6
3 5
4 8
5 0希望通过update #t 使Num2的每一行都等于前面行所有Num1的和即希望得到的结果是:
行号 Num1 Num2
1 5 5
2 6 11
3 5 16
4 8 24
5 0 24求update 语句
if object_id('tempdb.dbo.#t') is not null drop table #t
create table #t(行号 int, Num1 int, Num2 varchar(8))
insert into #t
select 1, 5,null union all
select 2, 6,null union all
select 3, 5,null union all
select 4, 8,null union all
select 5, 0,nullupdate t set Num2 = (select sum(Num1) from #t where 行号<=t.行号) from #t tselect * from #t/*
行号 Num1 Num2
----------- ----------- --------
1 5 5
2 6 11
3 5 16
4 8 24
5 0 24
*/
[/code]
if object_id('tempdb.dbo.#t') is not null drop table #t
create table #t(行号 int, Num1 int, Num2 varchar(8))
insert into #t
select 1, 5,null union all
select 2, 6,null union all
select 3, 5,null union all
select 4, 8,null union all
select 5, 0,nullupdate t set Num2 = (select sum(Num1) from #t where 行号<=t.行号) from #t tselect * from #t/*
行号 Num1 Num2
----------- ----------- --------
1 5 5
2 6 11
3 5 16
4 8 24
5 0 24
*/
[/code]
update a set Num2 = (select sum(Num1) from tb where 行号 <=a.行号) from tb a
[/code]
(
rowno int identity,
Num1 int,
Num2 int
)
insert #temp(Num1)
select 5 union all
select 6 union all
select 5 union all
select 8 union all
select 0declare @sum int
set @sum = 0
update #temp
set Num2 = @sum,
@sum = @sum + Num1[/code]