Daila
Daila

Reputation: 43

How to make a Delete Query inside a Trigger?

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>

Upvotes: 0

Views: 125

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172638

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

Upvotes: 1

Related Questions