删除触发器不起作用

时间:2013-07-18 16:14:07

标签: sql-server-2008 triggers

我的数据库中的表上有一个删除触发器,它应该将行插入“审计”表中,但是当我从触发器表中删除行时,它们不会显示在审计表中。我有插入和更新触发器遵循相同的格式,工作正常,但我似乎无法让这个工作。这是触发器:

USE [OutageDev]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tr_audOutageSummaryDelete]
ON [dbo].[Outage Summary]
AFTER DELETE
AS
  BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

      INSERT INTO dbo.[Outage Summary Audit]
                  (OutageSummaryID,
                   Dispatcher,
                   /*Removed columns for brevity*/
                   StateEmergency,
                   Comment,
                   AuditDateTime,
                   AuditUser,
                   AuditAction)
      SELECT d.OutageSummaryID,
             d.Dispatcher,
             /*Removed columns for brevity*/
             d.StateEmergency,
             t.Comment,
             GETDATE(),
             SUSER_SNAME(),
             'DELETE'
      FROM   deleted d
             JOIN [Outage Summary] t
               ON d.OutageSummaryID = t.OutageSummaryID
  END 

1 个答案:

答案 0 :(得分:0)

为了将来参考,我使用触发器表加入了已删除的表,以便选择类型为ntext的列,因为这是我的插入和更新触发器的工作方式。由于这是一个删除后触发器,我尝试选择的行已被删除。我将列从ntext转换为nvarchar(max)并删除了连接,现在它工作正常。