有问题的触发 - 删除

时间:2013-05-28 19:18:47

标签: sql sql-server sql-server-2008

我有一个跟踪已删除交易的触发器。它将事务记录前后的值记录到另一个名为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

1 个答案:

答案 0 :(得分:1)

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