0

I'm using SQL Server 2008 R2. I have a source table of data (I_Vendor) that may have duplicates on the CompanyName column. I want to import that data into a new table (Vendor) but the new table has a Name column (that corresponds to CompanyName) with a unique constraint on it. It's been a while since I've done SQL but I saw the MERGE function, and it appears it fits the bill. I wrote the following:

MERGE Vendor AS T
USING I_Vendor AS S
ON (T.Name = S.CompanyName) 
WHEN NOT MATCHED BY TARGET 
    THEN INSERT(VendorId, Name, ContactName, ContactInfoId) 
        VALUES(S.Vendor_ID, S.CompanyName, S.ContactName, S.Vendor_ID+10000);

It generates a "Violation of UNIQUE KEY constraint" and gives the name of the unique constraint on Vendor.Name. Anybody know what I'm doing wrong?

2
  • 1
    if in the source table there are two rows with same companyName and different contactname which contact name do you want in the Vendor table? similarly what about other columns? Commented Nov 4, 2011 at 0:48
  • Sorry for not being clearer. The ContactName doesn't really matter, I just need to get a clean list of all the Vendor names (plus I want to figure out how to use the Merge command). Commented Nov 4, 2011 at 5:14

1 Answer 1

1

Your MERGE statement will insert all rows from I_Vendor that do not have a matching row in the Vendor table.

For example, suppose there are two rows in the I_Vendor table with company name "X", and further suppose that company name "X" does not appear in the Vendor table, then both rows will be inserted into the Vendor table, violating the constraint.

To fix the problem, you need to ensure that there is only one row per company name in the source data of the MERGE statement. The following merge statement does this, but as Aditya Naidu has already pointed out, we don't know what you want to do when there multiple records with the same company name in the I_Vendor table:

MERGE Vendor AS T
USING (SELECT MAX(Vendor_ID), CompanyName, MAX(ContactName), MAX(ContactInfoId)
       FROM I_Vendor
       GROUP BY CompanyName) AS S
WHEN NOT MATCHED BY TARGET 
    THEN INSERT(VendorId, Name, ContactName, ContactInfoId) 
         VALUES(S.Vendor_ID, S.CompanyName, S.ContactName, S.Vendor_ID+10000);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the clear explanation and the working example with the MERGE command. I was thinking once the first x contact name came in the second x would not match. I've been away from SQL for too long and I forgot how to think in a set oriented way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.