簡體   English   中英

sql update語句與外鍵沖突

[英]sql update statement conflict with foreign key

我正在嘗試使用此存儲過程更新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

當我執行該查詢時,出現以下錯誤: 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'.

這真的很奇怪,因為我沒有更新“ PaymentStatusID”列,但我不知道為什么它使我在該列上發生沖突。 該列也設置為NULL,並且已經具有值。 我正在嘗試僅更新PostingStatusID字段。 知道是什么原因嗎? 在此先感謝Laziale

PaymentStatusId列中存儲的值在PaymentStatus表中不得存在。

確保PostingStatusIdPaymentStatusId是有效的ID並存在於適當的表中。

SELECT PaymentStatusId, PostingStatusId 
FROM Posting
WHERE PostingId = @PostingId

我建議運行以下命令:

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

您可能在此處標記的過帳表中有錯誤的數據。 它是如何到達的...我想可能是在填充數據並禁用WITH CHECK選項之后創建或重新創建了約束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM