Share via

T-SQL Cannot delete record although the source value of FK is null

Jan Vávra 76 Reputation points
2022-03-15T10:15:02.453+00:00

I have a table

CREATE TABLE [dbo].[XG_SFLF](
[ID_SFLF] [decimal](9, 0) IDENTITY(1,1) NOT NULL,
....
[ID_SFLF_NEXT] [decimal](9, 0) NULL,
CONSTRAINT [XG_SFLF_I1] PRIMARY KEY CLUSTERED
(
[ID_SFLF] ASC
)

CREATE NONCLUSTERED INDEX [XG_SFLF_I3] ON [dbo].[XG_SFLF]
(
[ID_SFLF_NEXT] ASC
)

ALTER TABLE [dbo].[XG_SFLF] WITH CHECK ADD CONSTRAINT [XG_SFLF_FK1] FOREIGN KEY([ID_SFLF_NEXT])
REFERENCES [dbo].[XG_SFLF] ([ID_SFLF])

This table contains a chain o versions of binary data references of one document.

I wanted to delete a record where ID_SFLF was 154 and ID_SFLF_NEXT was NULL.
On dev environment I've succeded but on prod I got:

"The DELETE statement conflicted with the SAME TABLE REFERENCE contraint "XG_SFLF_FK1". The conflict occured in database "xxxx", table "dbo.XG_SFLF", column 'ID_SFLF_NEXT'

I've also tried to drop the foreign key, but got some lock error ...
Is there any advice? Could be the foreign key somehow corrupted?

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


1 answer

Sort by: Most helpful
  1. Jan Vávra 76 Reputation points
    2022-03-15T10:30:11.647+00:00

    It was my bug. There was another record that was referencing value 154. The another record had ID_SFLF_NEXT = 154.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.