简体   繁体   中英

Stored procedure. Check if constraint are used in another table

I get this error message:

The DELETE statement conflicted with the REFERENCE constraint "FK_FieldMapper_Field". The conflict occurred in database "SCAM", table "dbo.FieldMapper", column 'FieldID'.

I have some accesshelpers which has a given amount of fields. These Fields can be used by multiple accesshelpers..

When I delete an accesshelper , I need to check if the fields in the given accesshelper are used by other accesshelpers . If they are, I shall delete the accesshelper, but not the fields, as that would break other accesshelpers. How do i do that?

This is what I have come up with on my own so far.

USE [SCAM]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[deleteAccessHelperById]

@Id int
AS
BEGIN

    SET NOCOUNT ON;

    DECLARE @projectIds TABLE (id int);
    INSERT INTO @projectIds ([id]) SELECT dbo.AccessHelperMapper.ProjectID FROM dbo.AccessHelperMapper WHERE dbo.AccessHelperMapper.AccessHelperID = @Id;

    DELETE dbo.AccessHelperMapper  WHERE dbo.AccessHelperMapper.AccessHelperID = @Id;

    DECLARE @fieldIds TABLE (id int);

    INSERT INTO @fieldIds ([id]) SELECT dbo.FieldMapper.FieldID FROM dbo.FieldMapper WHERE dbo.FieldMapper.AccessHelperID = @Id;


    DECLARE @AHfields TABLE (id int)
    Insert into @AHfields 
    Select fids.id from dbo.FieldMapper, @fieldIds as fids
    where dbo.FieldMapper.AccessHelperID != @Id
    and fids.id != dbo.FieldMapper.FieldID;

    delete dbo.FieldMapper where dbo.FieldMapper.AccessHelperID = @Id and dbo.FieldMapper.FieldID IN (SELECT d.id FROM @AHfields as d);
    delete dbo.Field where dbo.Field.ID in (SELECT g.id FROM  @AHfields as g);
    delete dbo.AccessHelper where dbo.AccessHelper.ID = @Id;


END

Try the below code. Hope this is what you were looking for:

USE [SCAM]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[deleteAccessHelperById]

@Id int
AS
BEGIN

SET NOCOUNT ON;

DELETE dbo.AccessHelperMapper  WHERE dbo.AccessHelperMapper.AccessHelperID = @Id;

DECLARE @fieldIds TABLE (id int);

INSERT INTO @fieldIds ([id]) SELECT dbo.FieldMapper.FieldID FROM dbo.FieldMapper WHERE dbo.FieldMapper.AccessHelperID = @Id
AND dbo.FieldMapper.FieldID NOT IN (SELECT DISTINCT dbo.FieldMapper.FieldID FROM dbo.FieldMapper WHERE dbo.FieldMapper.AccessHelperID != @Id);

delete dbo.FieldMapper where dbo.FieldMapper.AccessHelperID = @Id --and dbo.FieldMapper.FieldID IN (SELECT d.id FROM @fieldIds as d);
delete dbo.Field where dbo.Field.ID in (SELECT g.id FROM  @fieldIds as g);
delete dbo.AccessHelper where dbo.AccessHelper.ID = @Id;


END

First of all you need a transaction because you have partial data changes which cannot be applied separately.

ALTER PROCEDURE [dbo].[deleteAccessHelperById]
  @Id int
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @fieldIds TABLE (id int);

    BEGIN TRY
      BEGIN TRAN

      DELETE ahm
      FROM dbo.AccessHelperMapper ahm 
      WHERE ahm.AccessHelperID = @Id;

      DELETE fm
        OUTPUT DELETED.fieldID
        INTO @fieldIds(id)
      FROM dbo.FieldMapper fm
      WHERE fm.AccessHelperID = @Id 

      DELETE f
      FROM dbo.Field f
      WHERE exists(select 1 from @fieldIDs fid where fid.ID = f.ID)
        and not exists(
          select 1 from dbo.FieldMapper fm
          where fm.fieldID = f.ID 
             and fm.AccessHelperID != @ID  -- redundant, may be removed.
        )

      DELETE ah
      FROM dbo.AccessHelper ah
      where ah.ID = @Id

      COMMIT TRAN
    END TRY
    BEGIN CATCH
      IF XACT_STATE() IN (1, -1)
        ROLLBACK TRAN

      THROW;
    END CATCH
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM