Saturday, February 19, 2011

Sql Server Error 8951 Data row does not have a matching index row in the index



The error 8951 emerges most often when there is a duplicate row exists on PK. You would not be able to find the duplicate row

till you drop the PK and try as follows:

Let's Understand: There is a simple table EmployeeInfo where the PK is created on Empid, Rid, KeyInfo, DateTimeSlot columns. There are duplicate rows in the table for the columns on which PK is established but following query throws only one value for the following query even though two rows exist:

select Empid, Rid, KeyInfo, DateTimeSlot, count(1)
from Tuitionaffordable..EmployeeInfo
GROUP BY Empid, Rid, KeyInfo, DateTimeSlot
HAVING COUNT(1) > 1

Let's drop the PK and run the above query again. This shows me duplicate values for the columns on which PK is created. You need to filter the bad data out or modify these rows to avoid this info.

Let's again restore the same DB on a Test server and use following command for any specific value where this had duplicate rows:

select Empid, Rid, KeyInfo, DateTimeSlot, count(1)
from Tuitionaffordable..EmployeeInfo
where LTRIM(RTRIM(Empid)) = 97
and LTRIM(RTRIM(Rid))=2
and KeyInfo = 'ACAF387865567973E43C3ADB96C'
and DateTimeSlot= '2011-01-29 00:00:00.000'

This doesn't show the duplicate rows but following query will show you the data

select Empid, Rid, KeyInfo, DateTimeSlot,count(bit_delete)
from Tuitionaffordable..EmployeeInfo
where LTRIM(RTRIM(Empid)) = 97
and LTRIM(RTRIM(Rid))=2
and LTRIM(RTRIM(KeyInfo)) = 'ACAF31DBC69556EA9A973E43C3ADB96C'
and DateTimeSlot= '2011-01-29 00:00:00.000'
GROUP BY Empid, Rid, KeyInfo, DateTimeSlot

Following is the data:



Empid Rid KeyInfo DateTimeSlot (No column name)
97 2 ACAF387865567973E43C3ADB96C 2011-01-29 00:00:00.000 1
97 2 ACAF387865567973E43C3ADB96C 2011-01-29 00:00:00.000 1

The problem is in your DateTimeSlot column. drop the PK and this will catch where your duplicate data is.

Regards,
http://tuitionaffordable.webstarts.com

No comments:

Post a Comment