1

我正在尝试使用以下存储过程从数据库中删除父记录和子记录:

CREATE PROCEDURE [dbo].[DeleteCompanyPersonAndPhoneNumbers] (
    @personId int,
    @deleted bit output)
AS
BEGIN
    SET NOCOUNT ON;
    SET XACT_ABORT ON;

    set @deleted = 0

    begin transaction

        -- delete the phone numbers
        delete from PersonalPhoneNumber 
            where PersonalPhoneNumber.PersonId = @personId

        delete from ProfessionalProfile
            where ProfessionalProfile.Person_Id = @personId  

        delete from aspnet_UserProfile
            where aspnet_UserProfile.Person_ID = @personId

        delete from Accreditation
            where Accreditation.Person_ID = @personId

        delete from Qualification
                where Qualification.Person_ID = @personId

        delete from PERSON where Person_ID = @personId

        set @deleted = 1

    commit transaction

END

我已将外键强制关闭。当运行 sp 它只删除父记录。

我在 SP 中做错了什么?

谢谢

4

1 回答 1

3

您必须设置外键强制以通过级联删除删除子表中的记录。禁用外键也是糟糕的数据库设计,可能会给您带来麻烦。

于 2013-08-01T14:47:14.730 回答