创建SQL触发器以反转先前的触发器

时间:2017-12-29 22:06:12

标签: sql crystal-reports database-trigger

我正在尝试创建多个触发器。一个触发器更新了“已经确认”的消息。字段时(隐藏)字段'重新打印' Crystal Reports会以相同的形式更新。第二个触发器需要更新“重新打印”。字段如果' ysnAcknowledged'字段由用户手动更新(表单复选框)。我有第一个触发器,我的问题是如果我更新了'ysnAcknowledged'字段,它会在我想要它之前触发我的第二个触发器有效地反转第一个触发器。

第一次触发:

CREATE TRIGGER SOPickListReprint
ON dbo.SalesOrder
AFTER UPDATE
AS

IF UPDATE(Reprint)
UPDATE dbo.SalesOrder
SET ysnAcknowledged = 1

第二次触发:

CREATE TRIGGER SOPickListUpdate
ON dbo.SalesOrder
AFTER UPDATE
AS

IF UPDATE(ysnAcknowledged)
UPDATE dbo.SalesOrder
SET Reprint = 0

2 个答案:

答案 0 :(得分:1)

CREATE TRIGGER SOPickListUpdate
    ON dbo.SalesOrder
    AFTER UPDATE
AS

-- Update only rows where Reprint changed or ysnAcknowledged changed
-- > If ysnAcknowledged changed, force Reprint to 0, otherwise leave it as it is
-- > If Reprint changed, force ysnAcknowledged to 1, otherwise leave it as it is
UPDATE
    SalesOrder
SET
    Reprint         = CASE WHEN inserted.ysnAcknowledged <> deleted.ysnAcknowledged THEN 1 ELSE inserted.Reprint END,
    ysnAcknowledged = CASE WHEN inserted.Reprint         <> deleted.Reprint         THEN 0 ELSE inserted.ysnAcknowledged END
FROM
    SalesOrder
INNER JOIN
    inserted
        ON  inserted.<primary_key> = SalesOrder.<primary_key>
INNER JOIN
    deleted
        ON  deleted.<primary_key>  = SalesOrder.<primary_key>
WHERE
       inserted.ysnAcknowledged <> deleted.ysnAcknowledged
    OR inserted.Reprint         <> deleted.Reprint

答案 1 :(得分:0)

我的解决方案:

CREATE TRIGGER SOPickListReprint
ON dbo.SalesOrder
AFTER UPDATE
AS

UPDATE dbo.SalesOrder
SET ysnAcknowledged = CASE WHEN deleted.Reprint = 0 AND inserted.Reprint = 1 THEN 1 ELSE inserted.ysnAcknowledged END,
Reprint = CASE WHEN deleted.ysnAcknowledged = 1 AND inserted.ysnAcknowledged = 0 THEN 0 ELSE inserted.Reprint END
FROM SalesOrder
INNER JOIN
inserted
    ON  inserted.SalesOrderID = SalesOrder.SalesOrderID
INNER JOIN
deleted
    ON  deleted.SalesOrderID  = SalesOrder.SalesOrderID