在更新触发器上更新子表的每条记录

时间:2013-07-22 19:20:26

标签: sql-server-2008 ms-access triggers subform

我在Access中有一个表单,其中SQL Server中的链接表称为Outage Summary。有一些子表单,但只有一个称为停机详细信息是值得关注的。表单和子表单由名为OutageSummaryID的属性链接。每个中断摘要可以有多个中断详细信息,每个中断明细记录都有唯一的OutageDetailID

我有一个SQL Server中每个表的触发器,它记录执行的每个INSERTUPDATEDELETE,并将它们与dateTime一起添加到审计表中以及用户执行了哪些操作。

只要您编辑和更新任何停机摘要或添加和停机详细信息,停机详细信息审核表就会填充每条OutageDetailID的记录,表明记录已同时更新。我不相信Access实际上会更新表中的每条记录。也许我的触发器存在问题(尽管每个其他触发器的编写方式和功能都与预期相同)或者我正在做的事情存在根本性的缺陷。我该如何解决这个问题?

我的触发器:

USE [OutageDev]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tr_audOutageDetails]
ON  [dbo].[Outage Details SubTbl]
AFTER INSERT,UPDATE,DELETE

AS 

BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for trigger here
IF EXISTS (SELECT TOP 1 * FROM Inserted) AND EXISTS (SELECT TOP 1 * FROM Deleted)

    INSERT INTO dbo.[Outage Details Audit] (OutageSummaryID, [TimeOut], /* ... */ OutageDetailID, AuditDateTime, AuditUser, AuditAction)
        SELECT i.OutageSummaryID, i.[TimeOut], /* ... */ i.OutageDetailID, GETDATE(), SUSER_SNAME(), 'UPDATE'
        FROM inserted i join deleted d on i.OutageDetailID = d.OutageDetailID

IF EXISTS (SELECT TOP 1 * FROM Inserted) AND NOT EXISTS (SELECT TOP 1 * FROM Deleted)

    INSERT INTO dbo.[Outage Details Audit] (OutageSummaryID, [TimeOut], /* ... */ OutageDetailID, AuditDateTime, AuditUser, AuditAction)
        SELECT OutageSummaryID, [TimeOut], /* ... */ OutageDetailID, GETDATE(), SUSER_SNAME(), 'INSERT'
        FROM inserted 

IF NOT EXISTS (SELECT TOP 1 * FROM Inserted) AND EXISTS (SELECT TOP 1 * FROM Deleted)

    INSERT INTO dbo.[Outage Details Audit] (OutageSummaryID, [TimeOut], /* ... */ OutageDetailID, AuditDateTime, AuditUser, AuditAction)
        SELECT OutageSummaryID, [TimeOut], /* ... */ OutageDetailID, GETDATE(), SUSER_SNAME(), 'DELETE'
        FROM deleted 

END

我在UPDATE触发器上添加连接之前遇到了同样的问题,我只是将其添加到...我想进一步说服自己实际上正在删除和插入行。无论是否加入,它的功能都相同。

0 个答案:

没有答案