Hi experts,
I have yet another FK error issue when I try to delete.
The DELETE statement conflicted with the REFERENCE constraint "FK_tblRFISummary_tblRFIQuestion_tblID". The conflict occurred in table "dbo.tblRFIQuestions", column 'tblID'.
tblRFIQuestions has a FK on tblID in tblRFISummary:
ALTER TABLE [dbo].[tblRFIQuestions] WITH CHECK ADD CONSTRAINT [FK_tblRFISummary_tblRFIQuestion_tblID] FOREIGN KEY([tblID])
REFERENCES [dbo].[tblRFISummary] ([tblID])
GO
ALTER TABLE [dbo].[tblRFIQuestions] CHECK CONSTRAINT [FK_tblRFISummary_tblRFIQuestion_tblID]
Interestingly, I get the error on tblRFISummary whether I delete it before or after deleting from tblRFI Questions.
Any tips will be greatly appreciated. (No I haven't tried the CASCADE option)
Mike01, I should have provided my complete set of code.
SET QUOTED_IDENTIFIER ON;
Declare @CompareDate char(10)
Set @CompareDate = (Select CompareDate from OzTestCompareDate);
DELETE FROM RFIQuestions
FROM tblRFIQuestions RFIQuestions
INNER JOIN tblEstimate estimate ON Estimate.EstimateID = RFIQuestions.EstimateID
INNER JOIN tblQuote Q ON Q.QuoteID = Estimate.QuoteID
WHERE (Q.quostatus NOT IN ('Active', 'HOLD') AND Q.DownloadDate < @CompareDate);
94,379 deleted successfully
SET QUOTED_IDENTIFIER ON;
Declare @CompareDate char(10)
Set @CompareDate = (Select CompareDate from OzTestCompareDate);
DELETE FROM RFISummary
FROM tblRFISummary RFISummary
INNER JOIN tblQuote Q
ON Q.QuoteID = RFISummary.QuoteID
WHERE (Q.quostatus NOT IN ('Active', 'HOLD') AND Q.DownloadDate < @CompareDate);
** fails with The DELETE statement conflicted with the REFERENCE constraint "FK_tblRFISummary_tblRFIQuestion_tblID". The conflict occurred in table "dbo.tblRFIQuestions", column 'tblID'
NOTE: I have discovered that 81 tblIDs in tblRFISummary do NOT exist in tblRFIQuestions.
So I suppose I have corrupted data. Not sure how that could happen unless the FK is corrupted.
What are your thoughts? Thanks
mike01, I like your idea. This is what I ran:
SET QUOTED_IDENTIFIER ON;
Declare @CompareDate char(10)
Set @CompareDate = (Select CompareDate from OzTestCompareDate);
DELETE FROM RFISummary
FROM tblRFISummary RFISummary
INNER JOIN tblQuote Q ON Q.QuoteID = RFISummary.QuoteID
INNER JOIN tblRFIQuestions Questions RFISummary.tblID Questions.tblID`
WHERE (Q.quostatus NOT IN ('Active', 'HOLD') AND Q.DownloadDate < @CompareDate);
Got the same FK error
So I took it one step forther by checking existence of tblID in the other table:
.SET QUOTED_IDENTIFIER ON;
Declare @CompareDate char(10)
Set @CompareDate = (Select CompareDate from OzTestCompareDate);
DELETE FROM RFISummary
FROM tblRFISummary RFISummary
INNER JOIN tblEstimate estimate ON Estimate.EstimateID = RFISummary.EstimateID
INNER JOIN tblQuote Q ON Q.QuoteID = Estimate.QuoteID INNER JOIN tblRFIQuestions Questions on RFISummary.tblID = Questions.tblID
WHERE (Q.quostatus NOT IN ('Active', 'HOLD') AND Q.DownloadDate < @CompareDate);
the second one fails because you have already deleted the records from tblRFIQuestions. I would try something like this then
drop table if exists #t
go
Create table #t (tblID int)
Set @CompareDate = (Select CompareDate from OzTestCompareDate);
DELETE FROM RFIQuestions
FROM tblRFIQuestions RFIQuestions
INNER JOIN tblEstimate estimate ON Estimate.EstimateID = RFIQuestions.EstimateID
INNER JOIN tblQuote Q ON Q.QuoteID = Estimate.QuoteID output deleted.tblID into #t
WHERE (Q.quostatus NOT IN ('Active', 'HOLD') AND Q.DownloadDate < @CompareDate);
DELETE FROM RFISummary
FROM tblRFISummary RFISummary
join #t t
on RFISummary.tblID = t.tblID