我试图忽略薪水高于5000的员工的删除。我在after delete
表上使用Employee
触发器。如果删除的员工的薪水更高,则将其重新插入到Employee
表中。
CREATE TRIGGER T1 ON Employee
AFTER DELETE
AS
INSERT INTO Employee
SELECT *
FROM DELETED D
WHERE D.salary > 5000
有什么方法可以执行此操作而无需重新插入行?如何使用INSTEAD OF
触发器代替AFTER DELETE
?
答案 0 :(得分:0)
INSTEAD OF触发器可以决定要做什么,而不是像AFTER触发器那样面对更改的记录。这主要用于基于多个表且需要更新的视图。然后,触发器必须将更新定向到正确的基础表。
您可以的话
CREATE TRIGGER T1 ON Employee INSTEAD OF DELETE
AS
DELETE FROM Employee
FROM Employee e
INNER JOIN deleted d ON e.EmployeeID = d.EmployeeID
WHERE e.salary <= 5000
确保禁用递归触发器。