create trigger tr_process_InStock on InStock
for update
as
if update(BillConfirm) --如果是进行审核,才进行处理
update a set StockQTY=b.QTY
from Stock a,(
select b.Product,a.WhID
,QTY=sum(case
when a.BillConfirm=1 --为1表示审核,为0表示取消审核
then QTY
else -QTY end)
from inserted a,InStockDet b
where a.InStockNo=b.InStockNo
group by b.Product,a.WhID
)b where a.InStockNo=b.InStockNo and a.WhID=b.WhID
for update
as
if update(BillConfirm) --如果是进行审核,才进行处理
update a set StockQTY=b.QTY
from Stock a,(
select b.Product,a.WhID
,QTY=sum(case
when a.BillConfirm=1 --为1表示审核,为0表示取消审核
then QTY
else -QTY end)
from inserted a,InStockDet b
where a.InStockNo=b.InStockNo
group by b.Product,a.WhID
)b where a.InStockNo=b.InStockNo and a.WhID=b.WhID
create trigger tr_process_InStock on InStock
for update
as
if update(BillConfirm) --如果是进行审核,才进行处理
begin
select b.Product,a.WhID
,QTY=sum(case
when a.BillConfirm=1 --为1表示审核,为0表示取消审核
then QTY
else -QTY end)
into #t
from inserted a,InStockDet b
where a.InStockNo=b.InStockNo
group by b.Product,a.WhID --更新已经存在的
update a set StockQTY=b.QTY
from Stock a,#t b
where a.InStockNo=b.InStockNo and a.WhID=b.WhID --插入不存在的
insert Stock(Product,StockQTY,WhID)
select Product,QTY,WhID
from #t b
where not exists(
select * from Stock
where InStockNo=b.InStockNo and WhID=b.WhID)
end