日期:2014-05-17 浏览次数:20535 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (品名 nvarchar(4),区分 int,数量 int)
insert into [TB]
select 'AA',0,100 union all
select 'AA',1,200 union all
select 'XY',0,100 union all
select 'XY',1,300
select * from [TB]
BEGIN TRAN D1
UPDATE dbo.TB
SET TB.数量 = TB.数量 + B.数量
FROM (SELECT 品名,SUM(数量) AS 数量 FROM TB WHERE 区分 = 1 GROUP BY 品名 )B
WHERE B.品名 = TB.品名
and TB.区分 = 0
DELETE FROM TB WHERE 区分 = 1
SELECT * FROM dbo.TB
COMMIT TRAN D1
/*
AA 0 300
XY 0 400*/