34,590
社区成员
发帖
与我相关
我的任务
分享
SELECT ID,NAME,SUM(NUM) FROM TB GROUP BY ID,NAME
?
--这是触发器的方法,上帖发过了
create table goods (id int,name varchar(50),num int)
create trigger tri_goods
on goods
instead of insert
as
begin
update a set a.num=a.num+b.num
from goods a join inserted b on a.id=b.id and a.name=b.name
insert into goods(id,name,num) select id,name,num from inserted t where not exists(select 1 from goods with(nolock) where id=t.id and name=t.name)
end
insert into goods(id,name,num) select 1,'馒头',11
insert into goods(id,name,num) select 1,'馒头',20
insert into goods(id,name,num) select 1,'白菜',20
select * from goods