简体   繁体   English

sql update语句与外键冲突

[英]sql update statement conflict with foreign key

I am trying to update a sql table with this stored procedure: 我正在尝试使用此存储过程更新sql表:

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'. 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. 这真的很奇怪,因为我没有更新“ PaymentStatusID”列,但我不知道为什么它使我在该列上发生冲突。 That column is also set to NULL and it has value already. 该列也设置为NULL,并且已经具有值。 I am trying to update only PostingStatusID field. 我正在尝试仅更新PostingStatusID字段。 Any idea what can be the reason? 知道是什么原因吗? Thanks in advance, Laziale 在此先感谢Laziale

The value stored in the PaymentStatusId column must not exist in the PaymentStatus table. PaymentStatusId列中存储的值在PaymentStatus表中不得存在。

Ensure that PostingStatusId , PaymentStatusId are valid id's and exist in the appropriate tables. 确保PostingStatusIdPaymentStatusId是有效的ID并存在于适当的表中。

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. 它是如何到达的...我想可能是在填充数据并禁用WITH CHECK选项之后创建或重新创建了约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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