Help Creating Trigger

  • Hello, I am trying to create a trigger that will write a record to a table before a record is deleted. Currently, if a dlete is made, all records are being written to the other table instead of the one row that has been deleted. Below is my code, can someone help me understand what I need to revise?

    ALTER TRIGGER [dbo].[TrackDeletedEvents] ON [dbo].[MYEVENTS]

    FOR DELETE

    AS

    INSERT INTO DeleteTracker (EventID, EventDate)

    SELECT EID, StartDate

    FROM MYEVENTS

    How can I make this INSERT the one row that is actually deleted?

    Thank you in advance!

  • You want to select the deleted column values from the pseudo-table DELETED.

    INSERT INTO DeleteTracker (EventID, EventDate)

    SELECT deleted.EID, deleted.StartDate

    FROM deleted

    Here is a tutorial on creating an AFTER DELETE trigger that I think will help you out:

    http://www.tech-recipes.com/rx/41468/sql-server-coding-the-after-delete-trigger-in-sql-server/

  • thank you very much!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply