CREATE TRIGGER tri_PODetUpdate ON STN_PODet
FOR UPDATE
AS
begin
declare @pono varchar(30)--主键
declare @SourceNo varchar(20)
declare @SourceSerialNo int
declare @qty int--用来存储更新数量
declare @count int
SELECT @pono = PONO,@SourceNo = SourceNo,@SourceSerialNo = SourceSerialNo from deleted
select @qty = Qty from Inserted
if @SourceSerialNo > 0
begin--这样求和,其中肯定包括我现在更新的这条数据,那么取的是 更新前的呢,还是更新后的??(我希望是更新后的)
select @count = SUM(qty) FROM STN_PODet
WHERE SourceNo = @SourceNo AND SourceSerialNo = @SourceSerialNo update STN_SubscribeDet set DeliveryQty = @count
WHERE SubscribeNo = @SourceNo AND serialNo = @SourceSerialNo
end
end
GO那么如果上面语句改为:CREATE TRIGGER tri_PODetUpdate ON STN_PODet
FOR UPDATE
AS
begin
declare @pono varchar(30)
declare @SourceNo varchar(20)
declare @SourceSerialNo int
declare @qty int
declare @count int
SELECT @pono = PONO,@SourceNo = SourceNo,@SourceSerialNo = SourceSerialNo from deleted
select @qty = Qty from Inserted
if @SourceSerialNo > 0
begin
select @count = SUM(qty) FROM STN_PODet
WHERE SourceNo = @SourceNo AND SourceSerialNo = @SourceSerialNo
AND PONO <> @pono--此处修改 --此处修改
update STN_SubscribeDet set DeliveryQty = (@qty + @count)
WHERE SubscribeNo = @SourceNo AND serialNo = @SourceSerialNo
end
end
GO后是否可以?DeliveryQty 最终要取得的是STN_PODet 里面符合条件(WHERE SourceNo = @SourceNo AND SourceSerialNo = @SourceSerialNo)的Qty和(更新后的数据的SUM)
可能说的有点模糊,不过让我自己说,也不知道说什么了有什么模糊的就问我····
更新后的
因为修改的是PONO 列??
如果要判断更改哪列的话,用update(column)就可以了。
这样写,也不一定正确。
前面那个应该是更新后的。