简体   繁体   中英

sql update statement conflict with foreign key

I am trying to update a sql table with this stored procedure:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

CREATE PROCEDURE [dbo].[UpdatePostingStatusAngel]
     @PostingStatusID tinyint,
     @PostingID int
AS
UPDATE dbo.Posting
SET
    PostingStatusID = @PostingStatusID
WHERE PostingID = @PostingID

When I am executing that query I am getting this error: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Posting_PaymentStatus". The conflict occurred in database "JobsDB2008", table "dbo.PaymentStatus", column 'PaymentStatusID'. The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Posting_PaymentStatus". The conflict occurred in database "JobsDB2008", table "dbo.PaymentStatus", column 'PaymentStatusID'.

This is really weird because I am not updating 'PaymentStatusID' column I don't know why it gives me conflict on that column. That column is also set to NULL and it has value already. I am trying to update only PostingStatusID field. Any idea what can be the reason? Thanks in advance, Laziale

The value stored in the PaymentStatusId column must not exist in the PaymentStatus table.

Ensure that PostingStatusId , PaymentStatusId are valid id's and exist in the appropriate tables.

SELECT PaymentStatusId, PostingStatusId 
FROM Posting
WHERE PostingId = @PostingId

I would suggest running the following command:

SELECT *
FROM   dbo.Posting
WHERE  PaymentStatusId
NOT IN (
    SELECT PaymentStatusId
    FROM dbo.PaymentStatus
)

You probably have bad data in Posting table that is being flagged here. How it got in there...I would guess that the constraint was either created or recreated after the data was populated, and the WITH CHECK option was disabled.

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