Home > database >  How to make a Delete Query inside a Trigger?
How to make a Delete Query inside a Trigger?

Time:12-09

I am doing an after insert trigger on a table where I delete the rows that are incorrect, however, I am unable to do this and I can't find the correct syntax anywhere.

From what I understand, the table where the rows will be deleted from is in the FROM clause but I also need to use the inserted table. How do I do this? I know this can be solved using an instead of insert trigger but I really want to know how to do this way.

DELETE 
FROM promotion p, inserted i 
WHERE <conditions>

CodePudding user response:

You can try like this:

CREATE TRIGGER myTrigger ON dbo.MyTable
FOR INSERT
AS
SET NOCOUNT ON;
DELETE sometarget
FROM dbo.MyTable AS sometarget
JOIN inserted ON inserted.ID = sometarget.ID
WHERE <conditions>;
GO
  • Related