SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Receiving duplicate key insert error with DISTINCT clause


Receiving duplicate key insert error with DISTINCT clause

Author
Message
BrerSQL
BrerSQL
Old Hand
Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)

Group: General Forum Members
Points: 306 Visits: 578
I have been working on a bug for a couple days now which I have determined to be caused by the use of the NOLOCK in a query. Here is the query

Declare @AbsTab table (org_id int, absr_id int, abs_id int, abs_date datetime primary key (absr_id, abs_id,abstab_id))
Insert @AbsTab
select
a.org_id,
a.absr_id,
a.abs_id,
a.abs_date
from absence a (nolock)
where a.abs_date between @StartDate and @EndDate
and a.abs_deleted = 0
and a.abs_id not in (select abs_id from @CloseAbs)


I have determined that the (NOLOCK) is causing me problems because this query is using a covering index that includes updatable fields and we are seeing dirty reads. These dirty reads are causing duplicate key insert errors.

Msg 2627, Level 14, State 1, Line 117
Violation of PRIMARY KEY constraint 'PK__#4B31A61E__4C25CA57'. Cannot insert duplicate key in object 'dbo.@AbsTab'.

OK...I can understand this much and I can accept it, after all this is the sacrifice we have to make when we choose to use isolation level READ UNCOMMITTED.
My QUESTION, if I put a DISTINCT clause on the SELECT statement shouldn't this guarantee me unique rows?

Insert @AbsTab
select DISTINCT
a.org_id,
a.absr_id,
a.abs_id,
a.abs_date
from absence a (nolock)
where a.abs_date between @StartDate and @EndDate
and a.abs_deleted = 0
and a.abs_id not in (select abs_id from @CloseAbs)


I have tested this and I continue to receive errors.
I have written some code to trap the results being inserted into this table:

11053 31769404 46229984 2009-03-11 00:00:00.000
11053 31769404 46229984 2009-03-11 00:00:00.000

As you can see, the values are identical as far as I can tell.
Can anyone explain why the DISTINCT does not work in this case.

By the way, I am able to eliminate the error by adding an IDENTITY column to the table variable and making this column part of the PK. This does not get around the duplicate data but it does resolve the DUPLICATE KEY INSERT errors.
noeld
noeld
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23042 Visits: 2048
Are you sure only **one** insert is going at a time ?


* Noel
BrerSQL
BrerSQL
Old Hand
Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)

Group: General Forum Members
Points: 306 Visits: 578
Not sure exactly what your asking.
This is a table variable so all inserts are isolated to this process.
The plan is not taking advantage of parallelism.
Let me know if this does not answer your question.
Florian Reischl
Florian Reischl
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8975 Visits: 3934
Hi Eric

The NOLOCK may be your problem if there are very much data manipulation while your statement is running. Maybe have a look at:

SET TRANSACTION ISOLATION LEVEL SNAPSHOT

Greets
Flo


The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
bc_
bc_
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1684 Visits: 7333
When I try to run just your declare:
(Declare @AbsTab table (org_id int, absr_id int, abs_id int, abs_date datetime primary key (absr_id, abs_id,abstab_id)))

I get:
Msg 1911, Level 16, State 1, Line 1
Column name 'abstab_id' does not exist in the target table or view.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

I'm curious, one of the errors you got referred to 'dbo.@AbsTab'. Is that a "hard" table rather than a table variable?

bc

bc
BrerSQL
BrerSQL
Old Hand
Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)

Group: General Forum Members
Points: 306 Visits: 578
Sorry...my badBlush
remove abstabid from the PK
that was a remnant from my previous testing...jugling a lot of stuff
BrerSQL
BrerSQL
Old Hand
Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)

Group: General Forum Members
Points: 306 Visits: 578
@AbsTab is a declared table variable
BrerSQL
BrerSQL
Old Hand
Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)

Group: General Forum Members
Points: 306 Visits: 578
The table that I am querying is too hot of a table to turn snapshot processing on.
I am more curious as to why DISTINCT does not work here.
noeld
noeld
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23042 Visits: 2048
Nevermind I need some coffee :d


* Noel
bc_
bc_
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1684 Visits: 7333
Do you see dupes when you run just the Select DISTINCT?

select DISTINCT
a.org_id,
a.absr_id,
a.abs_id,
a.abs_date
from absence a (nolock)
where a.abs_date between @StartDate and @EndDate
and a.abs_deleted = 0
and a.abs_id not in (select abs_id from @CloseAbs)

I can think of no reason DISTINCT shouldn't do as its told.

bc

bc
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search