INSERT deleted values into a table before DELETE with a DELETE TRIGGER

前端 未结 1 382
南旧
南旧 2020-12-11 16:27

For some reason I can\'t find the exact answer that I need. I searched for at last 20 minutes in here.

I know it\'s simple. VERY simple. But I can\'t fire the trigge

相关标签:
1条回答
  • 2020-12-11 17:17

    Your problem is: this trigger fires AFTER the delete has already happened. So there is no more row in HashTags which you could join on!

    You need to use this trigger instead:

    ALTER TRIGGER [dbo].[HashTags_BeforeDelete]
        ON [dbo].[HashTags]
        FOR DELETE
    AS
      BEGIN
        INSERT INTO HashTagsArchive(Id, HashTagId, delete_date)
           SELECT 
               d.Id, d.HashTagId, GETUTCDATE() 
           FROM deleted d 
      END
    GO
    

    The Deleted pseudo table contains the whole row(s) that were deleted - no need to join on anything...

    Also: this trigger fires after the delete has happened - so you don't need to do anything yourself, inside the trigger - just insert those bits of information into your archive table - that's all. Everything else is handled by SQL Server for you.

    0 讨论(0)
提交回复
热议问题