0

我有一个跟踪已删除事务的触发器。它将之前和之后的事务记录值存储到名为 TransHistory 的不同表中。问题是,当我删除一条记录时,它不仅存储已删除的记录,而且在此之前它会为任何客户插入我的 DataGridView 中存在的顶部记录作为更新。这是我的删除触发器:

USE [dbPB]
GO
/****** Object:  Trigger [dbo].[delete_history]    Script Date: 05/28/2013 19:40:13       ******/
 SET ANSI_NULLS ON
 GO
 SET QUOTED_IDENTIFIER On
 GO

 ALTER TRIGGER [dbo].[delete_history] ON [dbo].[Transaction]
 FOR DELETE
 AS
 INSERT TransHistory (CustomerID, TransactionID, Buyin, 
                        Cashout, CreditPaid, Type,
                         Date, action)
 SELECT CustomerID, TransactionID, Buyin, Cashout, 
   CreditPaid, Type, GETDATE(), 'DELETED' 
 FROM deleted

这是我的 UPDATE 触发器:

 USE [dbPB]
 GO
 /****** Object:  Trigger [dbo].[update_history]    Script Date: 05/28/2013 20:24:28      ******/
 SET ANSI_NULLS ON
 GO
 SET QUOTED_IDENTIFIER ON
 GO
 ALTER TRIGGER [dbo].[update_history] ON [dbo].[Transaction]
 FOR UPDATE
 AS
 INSERT TransHistory (CustomerID, TransactionID, Buyin, 
                         Cashout, CreditPaid, Type,
                         Date, action)
 SELECT top (1) CustomerID,TransactionID, Buyin, Cashout, CreditPaid, Type,
             GETDATE(), 'BEFORE UPDATE' 
 FROM deleted

 INSERT TransHistory (CustomerID, TransactionID, Buyin, Cashout,
                         CreditPaid, Type, 
                         Date, action)
 SELECT top(1) CustomerID, TransactionID, Buyin, Cashout, CreditPaid, Type,
             GETDATE(), 'AFTER UPDATE' 
 FROM inserted
4

1 回答 1

1

我认为您正在发送更新和删除。当触发器被删除调用时,我会使用分析器来查看发送到数据库的确切内容。您可以尝试从 SSMS 而不是应用程序执行删除语句,您可能会在 Profiler 中看到仅发送了删除语句并检查数据以查看是否仅触发了删除触发器。然后从应用程序中执行相同的操作,并看到发送了两个语句而不是一个。至少这会告诉你问题是触发器还是应用程序,但我赌的是应用程序。

于 2013-05-28T20:12:08.620 回答