SQL Server How to make a Delete Query inside a Trigger?

bjp0bcyl  于 2022-12-10  发布在  其他
关注(0)|答案(1)|浏览(166)

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>
ars1skjm

ars1skjm1#

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

相关问题