我正在尝试创建一个触发器,在删除实际记录之前删除所有外键。
问题在于,当我删除记录时,它会在SQL Server Management Studio中消失,但当我刷新页面时,我又找到了它。谁知道问题出在哪里?
create Trigger deleteAffaire on AFFAIRE INSTEAD OF DELETE As
begin
Declare @deletedAffaireId int
Declare @deletedPlanId int
set @deletedPlanId = 0;
select @deletedAffaireId = id_affaire from deleted;
begin
select @deletedPlanId = ID_ASSURENCE
from ASSURANCE_QUALITE
where ASSURANCE_QUALITE.ID_AFFAIRE = @deletedAffaireId;
delete from ASSURANCE_CONTACT_FONCTION
where ASSURANCE_CONTACT_FONCTION.ID_ASSURANCE = @deletedPlanId;
delete from JALON
where JALON.ID_JALON in (select ID_JALON from ASSURANCE_JALON_TYPE
where ASSURANCE_JALON_TYPE.ID_ASSURENCE = @deletedPlanId);
delete from ASSURANCE_JALON_TYPE
where ASSURANCE_JALON_TYPE.ID_ASSURENCE = @deletedPlanId;
delete from DOCUMENT
where DOCUMENT.ID_DOCUMENT in (select ID_DOCUMENT from ASSURENCE_DOCUMENT_TYPE
where ASSURENCE_DOCUMENT_TYPE.ID_ASSURENCE = @deletedPlanId);
delete from ASSURENCE_DOCUMENT_TYPE
where ASSURENCE_DOCUMENT_TYPE.ID_ASSURENCE = @deletedPlanId;
delete from ASSURANCE_QUALITE
where ASSURANCE_QUALITE.ID_ASSURENCE = @deletedPlanId;
end
end
答案 0 :(得分:5)
您正在INSTEAD OF DELETE
表上执行AFFAIRE
触发器,但在触发器主体中,您实际上无法从AFFAIRE
中删除任何内容。
INSTEAD OF
触发器不是'之前'触发器。顾名思义,是一个而不是触发器。如果你没有真正删除触发器中的行,那么......它们不会被删除。
您确定使用更常规的方法无法达到您想要的效果,例如在外键上级联删除吗?
答案 1 :(得分:0)
删除最后在触发器中尝试删除的记录。由于它是a而不是触发器,除非你在触发器中明确地执行它,否则它实际上不会被删除。