还是 SQL 问题。
两张表Table A
(
id int not null,
amt decimal(18,2),
constraint PK_A primary key ( id )
)
Table B
(
id int not null,
amt decimal(18,2),
workdate char(10) not null,
constraint PK_B primary key (id,workdate)
)
A.id 与 B.id 外键关联。
要求:一句话用 sum(B.amt) 修改 A.amt 中的值。
修改条件为A.id = B.id
=====================================
我目前的做法是对 A 表建立 ID 游标
根据 ID 汇总 B 表的 amt ,然后用汇总的 amt 替换 a 表的 amt 值。
但我感觉应该不用这么麻烦,可以一句话完成的。
:em17: :em17: :em17: 1、使用存储过程
2、引入临时表
一个语句应该可以实现,但那个会让人读不明白,而且容易出现意外;让你接班的人很容易很清楚地看懂你的实现过程比写出冗长晦涩的语句来得更重要。 merge into a
using ( select id,sum(amt) sumamt
from b
group by b ) b
on a.id = b.id
when matched then update set a.amt = b.sumamt;
页:
[1]