CREATE TRIGGER myDelete ON dbo.Employees INSTEAD OF DELETE AS BEGIN -- The table [Deleted] will contain all the records that have been set for deletion although you can't alter -- these directly so we join them to the real table UPDATE dbo.[Employees] SET dbo.[Employees].[Deleted] = 1 FROM dbo.[Employees] INNER JOIN [Deleted] ON dbo.[Employees].[ID]=[Deleted].[ID] END
As usual you are given a table (in this case DELETED) which holds the records of the items that have been marked for deletion. You're not allowed to modify this table directly so you have to inner join back to the original table and do what you need to. As you can see from the example above we are merely setting a flag column called [Deleted] rather than actually deleting the record, something that would not be possible with the DELETE Trigger alone, all hail INSTEAD OF.
No comments:
Post a Comment