我编写了一个UPDATE触发器来查找Test.dbo.receiving_podio_dump
表中的更改,然后执行以下两种不同的方案之一:
如果存在receiving_id
和现有sku_no
匹配且这是Test.dbo.receiving_detail
表中该组合的更新,则相应的行会更新,或者
如果存在receiving_id
但新sku_no
项,则需要在Test.dbo.receiving_detail
表中添加/插入新行。
这是我的触发器:
CREATE TRIGGER trigger_receiving_detail_sku1_update
ON [Test].[dbo].[receiving_podio_dump]
FOR UPDATE
AS
BEGIN
--UPDATES DATA IN THE ROW FOR THAT RECEIVING LOG/SKU NO COMBO IF IT ALREADY EXISTS--
IF (SELECT ISNULL(COUNT(*), 0)
FROM inserted
LEFT JOIN dbo.receiving_detail AS rec ON (inserted.receiving_id = rec.receiving_id AND '001' = rec.sku_no)) <> 0
BEGIN
UPDATE [dbo].[receiving_detail]
SET receiving_assigned_id = inserted.receiving_assigned_id,
sku_id = sku1_id, sku = sku1, sku_lot = sku1_lot,
sku_whloc = sku1_whloc, sku_weight_of_unit = sku1_weight_of_unit,
sku_unit_of_weight_type = sku1_unit_of_weight_type,
sku_quantity_of_units = sku1_quantity_of_units,
sku_unit_of_measure = sku1_unit_of_measure,
sku_temperature = sku1_temperature
FROM inserted
WHERE ([dbo].[receiving_detail].receiving_id = inserted.receiving_id
AND [dbo].[receiving_detail].sku_no = '001')
END
--INSERTS A ROW FOR A NEW SKU NO FOR AN EXISTING RECEIVING LOG NUMBER--
IF (SELECT ISNULL(COUNT(*), 0)
FROM inserted
LEFT JOIN dbo.receiving_detail AS rec ON (inserted.receiving_id = rec.receiving_id AND '001' = rec.sku_no)) = 0
BEGIN
INSERT INTO [dbo].[receiving_detail] (receiving_id, receiving_assigned_id, sku_id, sku_no, sku, sku_lot, sku_whloc, sku_weight_of_unit, sku_unit_of_weight_type, sku_quantity_of_units, sku_unit_of_measure, sku_temperature)
SELECT
inserted.receiving_id, inserted.receiving_assigned_id, sku1_id,
'001', sku1, sku1_lot, sku1_whloc, sku1_weight_of_unit,
sku1_unit_of_weight_type, sku1_quantity_of_units,
sku1_unit_of_measure, sku1_temperature
FROM
inserted
WHERE
sku1 IS NOT NULL
END
END
GO
触发器的第一部分正在运行,如果Test.dbo.receiving_detail
表中有更新,则会在Test.dbo.receiving_podio_dump
表上更新产品数量。
但是,如果添加了一个全新的SKU,则不会在我的Test.dbo.receiving_detail
表中插入新行。
我不知道要改变什么,我尝试了几件事,欢迎任何建议并表示赞赏。谢谢!
PS:接收日志共有24个可能被添加或更新的SKU项目。我只包含了第一个触发器,但它们都遵循这种模式。
答案 0 :(得分:0)
我删除了两个IF语句,并在UPDATE和INSERT语句中添加了这些条件,以支持大规模更新的情况,两个条件都为真。
CREATE TRIGGER trigger_receiving_detail_sku1_update
ON [Test].[dbo].[receiving_podio_dump]
FOR UPDATE
AS
BEGIN
--UPDATES DATA IN THE ROW FOR THAT RECEIVING LOG/SKU NO COMBO IF IT ALREADY EXISTS--
UPDATE rec
SET receiving_assigned_id = i.receiving_assigned_id,
sku_id = sku1_id, sku = sku1, sku_lot = sku1_lot,
sku_whloc = sku1_whloc, sku_weight_of_unit = sku1_weight_of_unit,
sku_unit_of_weight_type = sku1_unit_of_weight_type,
sku_quantity_of_units = sku1_quantity_of_units,
sku_unit_of_measure = sku1_unit_of_measure,
sku_temperature = sku1_temperature
FROM inserted i
INNER JOIN [dbo].[receiving_detail] rec ON rec.receiving_id = i.receiving_id
WHERE (rec.sku_no = '001')
--INSERTS A ROW FOR A NEW SKU NO FOR AN EXISTING RECEIVING LOG NUMBER--
INSERT INTO [dbo].[receiving_detail] (receiving_id, receiving_assigned_id, sku_id, sku_no, sku, sku_lot, sku_whloc, sku_weight_of_unit, sku_unit_of_weight_type, sku_quantity_of_units, sku_unit_of_measure, sku_temperature)
SELECT
i.receiving_id, i.receiving_assigned_id, sku1_id,
'001', sku1, sku1_lot, sku1_whloc, sku1_weight_of_unit,
sku1_unit_of_weight_type, sku1_quantity_of_units,
sku1_unit_of_measure, sku1_temperature
FROM
inserted i
WHERE sku1 IS NOT NULL
AND NOT EXISTS (SELECT * FROM [dbo].[receiving_detail] rec WHERE rec.receiving_id = i.receiving_id AND rec.sku_no = '001')
END
GO