请大家看下面的程序:
create table #temp1
(
seq_no int identity(1,1),
amount numeric(18,2)
);create table #temp2
(
seq_no int ,
amount numeric(18,2)
);insert into #temp1
values(10000);insert into #temp2 values(1,1000);
insert into #temp2 values(1,1000);
insert into #temp2 values(1,1000);
insert into #temp2 values(1,1000);
insert into #temp2 values(1,1000);
update a
set a.amount = a.amount + b.amount
from #temp1 a inner join #temp2 b
on a.seq_no = b.seq_no;select * from #temp1;
select * from #temp2;drop table #temp1;
drop table #temp2;我的想法是取出来的#temp1是15000,但是事与愿违,想请教一下大家会如何做,不要用游标的

解决方案 »

  1.   

    update a 
    set a.amount = 
    a.amount + (SELECT SUM(b.amount ) FROM #TEMP2  WHERE a.seq_no = seq_no)
    from #temp1 a inner join #temp2 b 
    on a.seq_no = b.seq_no; ?
      

  2.   

    #temp1是15000 ,15000是怎么来的?直接对#temp2进行SUM(amount)?
    不懂
      

  3.   

    update a 
    set a.amount = (SELECT SUM(b.amount ) FROM #TEMP2  WHERE a.seq_no = seq_no)
    from #temp1 a
      

  4.   

    把#temp2按seq_no取sum再根据seq_no与#temp1连接相加不行吗??
      

  5.   

    update a 
    set a.amount = 
    a.amount + (SELECT SUM(b.amount ) FROM #TEMP2 B WHERE a.seq_no =B.seq_no)
    from #temp1 a