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