Search code examples
sqlsql-serverstored-proceduresssmssql-delete

DELETE statement conflicted with Reference constraints in SQL Stored Procedure


My Employee table has the Primary key Column that is (EmpID) which is referenced in two Other tables EmployeeEducation table and EmployeeBankInformation table.

I want to Delete Employee against their ID but as I run the Stored Procedure my controller throws the error that the foreign key reference constraint conflicts because of the EmpID column.

ALTER PROCEDURE [dbo].[RemoveEmployee]
(
    @EmpID int
)
AS
BEGIN
    DELETE FROM Employee WHERE EmpID = @EmpID
    DELETE FROM EmployeeEducation WHERE EmpID = @EmpID
    DELETE FROM EmployeeBankInformation WHERE EmpID = @EmpID
END

Where am I going wrong? Can any of you tell what mistake I am making in my procedure?


Solution

  • You are very close, just need to remove the employee last:

    ALTER PROCEDURE [dbo].[RemoveEmployee]
    (
        @EmpID int
    )
    AS
    BEGIN
        DELETE FROM EmployeeBankInformation WHERE EmpID = @EmpID
        DELETE FROM EmployeeEducation WHERE EmpID = @EmpID
        DELETE FROM Employee WHERE EmpID = @EmpID
    END