SQL SERVER: MERGE TABLE WHILE IGNORING DUPLICATES

Dear sirs
I don't really know what wrong with this statement
It is giving this error message: Cannot insert duplicate key row in object 'dbo.quotes' with unique index 'spx_quotes'. The duplicate key value is (005169). An unique index is set on quoteno

MERGE INTO dbo.quotes AS TGT
USING DBSRC.dbo.quote AS SRC
ON (TGT.quoteno=SRC.quoteno)
WHEN MATCHED THEN
      DELETE
WHEN NOT MATCHED BY TARGET THEN
      INSERT (quoteno, userID)
      VALUES (SRC.quoteno, SRC.userID);
Omer-PitouAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
Using the duplicate quoteno='005196' is in the target

Here is a reference to merge https://technet.microsoft.com/en-us/library/bb522522(v=sql.105).aspx

You might want to change the order to insert/update TGT before .........
Omer-PitouAuthor Commented:
The message was clear about the duplicated row. This makes sense and that is why I am using MERGE to avoid that, and has included DELETE When matched.

I changed the order Insert/update as you suggested several times, add HoldLock clause, Serialize clauses, without success.

The source table has about 40000 rows.

I wonder if this isn't a 2008R2 limitation, being corrected in 2012.
I am using as we speak 2008R2.
arnoldCommented:
You have your unique/index quoteno. Is quoteno
005196 suggest that it is not a numeric definition.i.e 5196 exists in the TGT, but 005196 is what exists in the SRC.
THE MERGE does not match, but on insert the 005196 is converted to a number which then triggers a duplicate event.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arnoldCommented:
One thought, use
ON (cast(TGT.quoteno as int) =cast(SRC.quoteno as int))
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.