SQL Server: "cannot bulk load" error during delete
1
vote
0
answers
94
views
I am having a very *interesting* error using SQL Server 2022 and 2017. I am currently implementing my jurisdiction's data retention policies, which means doing a lot of deletes. I have run into a strange situation where a single, specific delete statement is causing an error that is leaving me stuck.
Because of the complexity of the code, and the fact that I tracked the error down to a single delete statement, and the fact that the error still happens when I do the simplest possible (bulk) delete, I won't be posting much code. I do hope that the reader will suggest strategies for approaching my problem.
Unfortunately, I don't have very much to work with. The error message doesn't tell me the relevant entities, so I am stuck guessing. And because there is an error, I don't get to see the execution plan for the delete that fails.
-------------------
In short, I have a table called
dbo.Person
. It has SYSTEM_VERSIONING set to ON, and the corresponding history table is called dbo.PersonHistory
. dbo.PersonHistory
incidentally, has an index on it. dbo.Person
also has a couple of indexes on it.
In the course of investigating my real issue, I simplified my buggy delete statement down to:
delete
from dbo.Person
;
and I get the error:
> Cannot bulk load. The bulk data stream was incorrectly specified as sorted or the data violates a uniqueness constraint imposed by the target table. Sort order incorrect for the following two rows: primary key of first row: (2025-03-18 23:20:18.5852411, 2021-07-02 18:25:01.9834390, redacted ID, 0), primary key of second row: (2025-03-18 23:20:18.5852411, 2021-07-02 03:31:22.7446737, redacted ID, 1)
It is not clear to me what the "redacted ID" is supposed to correspond to, since the error does not tell me what the target table is. But I examined all the indexes and similar on both the main and history tables and *guess* that these values "correspond" to values stored in the index on the history table, which would actually be a PersonId
, the PK on the dbo.Person
table. *If* this is true, my guess is that the delete is having to make changes to the history table's index and that change is breaking a constraint.
But if I search for a dbo.Person
row with that redacted ID, I find a single row. There are no rows in the history table for that person.
Does anybody have any suggestions to investigate? The error is so uninformative, and the lack of an actual execution plan makes it hard to debug the issue.
Thank you!
---------------------
It appears there are two indexes on the history table:
CREATE NONCLUSTERED INDEX [IX_PersonHistory_ID_PERIOD_COLUMNS_I]
ON [dbo].[PersonHistory]([SysEndTime] ASC, [SysStartTime] ASC,
[PersonId] ASC) WITH (DATA_COMPRESSION = PAGE);
GO
CREATE CLUSTERED COLUMNSTORE INDEX [CSIX_PersonHistory]
ON [dbo].[PersonHistory];
---------------------------------------
I dropped both indexes on the history table (in my dev database) and the error occurred again.
Asked by nomen
(113 rep)
Mar 19, 2025, 04:43 PM
Last activity: Mar 19, 2025, 09:33 PM
Last activity: Mar 19, 2025, 09:33 PM