我有物理表来保存ID,以及插入,删除,更新数据的时间 并使用基表附加我的触发器(插入,更新和删除后)。它是一种跟踪。
插入的记录和删除的记录很好,但问题是我没有在基表中获取更新记录的任何记录! 插入和删除的记录。
如果我在这里遗漏任何东西,请帮助我。
触发插入和更新操作:
CREATE TRIGGER [dbo].[trArticleAttachmentTrack]
ON [dbo].[ArticleAttachment]
AFTER INSERT, UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
INSERT INTO TrackArticleAttachmentTbl (ArticleAttachmentID, ActionOn)
SELECT i.ArticleAttachmentID, GETDATE() FROM inserted i
END
GO
触发删除操作:
CREATE TRIGGER [dbo].[trArticleAttachmentTrackDelete]
ON [dbo].[ArticleAttachment]
AFTER DELETE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
INSERT INTO TrackArticleAttachmentTblRowDel (ArticleAttachmentID, ActionOn)
SELECT i.ArticleAttachmentID, GETDATE() FROM deleted i
END
GO
查询以获得输出
SELECT
CASE WHEN i.ArticleAttachmentID IS NULL THEN 'This is a delete'
WHEN d.ArticleAttachmentID IS NULL THEN 'This is an insert'
ELSE 'This is an update'
END as Action,
*
FROM
TrackArticleAttachmentTbl i
FULL OUTER JOIN
TrackArticleAttachmentTblRowDel d
ON i.ArticleAttachmentID = d.ArticleAttachmentID
输出:
This is an update 38 18226 2012-02-19 14:01:52 5 18226 2012-02-19 14:02:01
This is an update 22 18210 2012-02-15 16:38:39 4 18210 2012-02-15 16:39:29
This is an update 13 18201 2012-02-15 12:21:22 2 18201 2012-02-15 12:21:32
This is an update 47 18235 2012-02-19 16:37:54 6 18235 2012-02-19 17:09:53
查询以获取更新记录
SELECT *
FROM dbo.ArticleAttachment
WHERE ArticleAttachmentID IN (18226,18210,18201,18235)
没有结果:(
答案 0 :(得分:0)
CASE表达式无法正确检测删除。试试这个。
CASE WHEN d.ArticleAttachmentID IS NOT NULL
THEN 'This is a delete'
ELSE 'This is an insert or update'
END