34,590
社区成员
发帖
与我相关
我的任务
分享
if exists(select 1 from inserted where bdid = 12) and not exists(select 1 from deleted) --insert
update tpDingDanMX
set sl_ = isnull(sl_,0)+ isnull(b.sl,0)
from (select i.ddmx_rowid,sum(isnull(i.sl,0)) as sl
from inserted i
group by i.ddmx_rowid) b
where tpDingDanMX.rowid = b.ddmx_rowid
if exists(select 1 from inserted where bdid = 12) and exists (select 1 from deleted) --update
begin
update tpDingDanMX
set sl_ = isnull(sl_,0)+ isnull(b.sl,0)
from
(select t.ddmx_rowid,sum(isnull(t.sl,0)) as sl
from (select i.ddmx_rowid,sum(isnull(i.sl,0)) as sl
from inserted i
group by i.ddmx_rowid
union all
select d.ddmx_rowid,sum(isnull(d.sl,0)) * (-1) as sl
from deleted i
group by d.ddmx_rowid) t
group by t.ddmx_rowid) b
where tpDingDanMX.rowid = b.ddmx_rowid
end
if not exists(select 1 from inserted where bdid = 12) and exists (select 1 from deleted where bdid = 12) --delete
update tpDingDanMX
set sl_ = isnull(sl_,0) - isnull(b.sl,0)
from (select d.ddmx_rowid,sum(isnull(d.sl,0)) as sl
from deleted i
group by d.ddmx_rowid) b
where tpDingDanMX.rowid = b.ddmx_rowid
if exists(select 1 from inserted where bdid = 12) and not exists(select 1 from deleted) --insert
update tpDingDanMX set sl_ = isnull(sl_,0)+ isnull(b.sl,0)
from tpDingDanMX a join inserted b on a.rowid = b.ddmx_rowid
if exists(select 1 from inserted where bdid = 12) and exists (select 1 from deleted) --update
begin
if exists(select 1 from inserted a join deleted b on a.rowid = b.rowid where a.sl <> b.sl)
update tpDingDanMX set sl_ = isnull(sl_,0) + (isnull(b.sl,0)- isnull(c.sl,0))
from tpDingDanMX a join inserted b on a.rowid = b.ddmx_rowid
join deleted c on b.rowid = c.rowid
end
if not exists(select 1 from inserted where bdid = 12) and exists (select 1 from deleted where bdid = 12) --delete
update tpDingDanMX set sl_ = isnull(sl_,0) - isnull(b.sl,0)
from tpDingDanMX a join deleted b on a.rowid = b.ddmx_rowid
update 库存表
set 库存数=库存数 - t.出库数
from (select i.产品编码,sum(i.出库数) as 出库数
from inserted i
group by i.产品编码)t
where t.产品编码= 库存表.产品编码