Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

SQL Merge Into Help

SQL Merge Into Help

SQL Merge Into Help


I use the following script to merge data from one table into another. This works fine 99% of the time. The issue I am having is that on occasion the data in in the source table pc_wiredorder holds multiple records with the same customerid, which is merged into the destination table wce_contact's uniqueid column which is the primary key field and therefore can't hold duplicate id's spo the script fails.

Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'pk_wce_contact_uid'. Cannot insert duplicate key in object 'dbo.wce_contact'.
The statement has been terminated.

Example merge data below that causes the issues, this customerid is in the source table twice and I need to merge/insert into the wce_contact table. There could be more rows with the same customerid. Ideally I need the script to see the duplication, and handle one row at a time. Then bring the 2nd/3rd row in. The script is scheduled to run every 5 mins so the 2nd row could be handled then or maybe I could use this script and put an if clause in or something?

Any guidance would be really helpful. Thanks.

id ------------ CustomerId ------------ AddedToWired
28903 ------------ 42936 ------------ 0
28904 ------------ 42936 ------------ 0


MERGE INTO wce_contact AS Target
using (select * from pc_wiredorder where addedtowired = '0') AS Source ON Target.emailaddress = Source.BillingEmail
    INSERT ([uniqueid], [createuser], [edituser], [recordmanager], [createtime], [edittime], [COMPANY], [contact], [FirstName], [lastname], [emailaddress], [Phone], [Address1], [Address2], [Address3], [City], 
    [County], [Postalcode], [Customer_Type], [customerpipeline], [account_manager], [Referred_By], record_type, OP_Gone_Ahead) 
    VALUES (source.[customerid], 'ykn3t293i7ckyat7', 'ykn3t293i7ckyat7', 'ykn3t293i7ckyat7', getdate(), getdate(), source.[FirstName]+' '+ source.[Surname], source.[FirstName]+ ' ' + source.[Surname], source.[FirstName], source.[Surname], source.[BillingEmail], source.[Phone], 
    source.[AddressLine1], source.[AddressLine2], source.[AddressLine3], source.[TownCity], source.[County], source.[Postcode], 
    source.[CustomerType], source.[CustomerPipeline], source.[AccountManager], 'Web Integration', 'Individual', 'Confirmed'); 

RE: SQL Merge Into Help

The data seems to use customerid as foreign key and you try to make contacts from it. Merge isn't working that way. Just pick out the columns you want to use from pc_wiredorder grouped by customerid.

Bye, Olaf.

Olaf Doschke Software Engineering

RE: SQL Merge Into Help

Thanks Olaf.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close