0

我正在尝试创建一个触发器,在删除实际记录之前删除所有外键。

问题是,当我删除一条记录时,它在 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 
4

2 回答 2

5

您正在INSTEAD OF DELETE表上执行触发器,AFFAIRE但在触发器主体中您实际上无法从中删除任何内容AFFAIRE

INSTEAD OF触发器不是“之前”触发器。顾名思义,是一个代替触发器。如果您未能实际删除触发器中的行,那么......它们不会被删除。

你确定你不能使用更传统的方式来实现你想要的,比如在外键上级联删除?

于 2013-11-15T08:55:19.277 回答
0

最后删除您试图在触发器中删除的记录。由于它不是触发器,因此除非您在触发器中明确执行,否则它实际上不会删除。

于 2013-11-15T15:02:33.620 回答