how do you update rows and not violate a PK on the table?

i have the following SQL:

UPDATE Products_ProductComponents
SET Products_ProductComponents.ComponentID = products_list_ORIG.derivedFrom
FROM Products_ProductComponents
      INNER JOIN products_list_ORIG ON products_list_ORIG.ProductID = Products_ProductComponents.ComponentID
WHERE products_list_ORIG.derivedFrom <> 0

the updated sets values in the PM that already exist and i get this error:
Violation of PRIMARY KEY constraint 'PK_Products_ProductComponents'. Cannot insert duplicate key in object 'dbo.Products_ProductComponents'.

how do i get around this?
i am thi nking i can update only records that will not violate the PK
or remove the PK, then delete dups then add PK back.

ideas?
supergirl2008Asked:
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.

ErnariashCommented:
Hello,
What is the PK_Products_ProductComponents?
If it is only Products_ProductComponents.ComponentID, and If you can update only records that will not violate the PK here is the SQL:
If you need to update Products_ProductComponents.ComponentID, delete the rows and reinsert with the new values.

UPDATE Products_ProductComponents
SET Products_ProductComponents.ComponentID = products_list_ORIG.derivedFrom
FROM Products_ProductComponents
      INNER JOIN products_list_ORIG 
      ON products_list_ORIG.ProductID = Products_ProductComponents.ComponentID
WHERE products_list_ORIG.derivedFrom <> 0 and not exists (
	SELECT 1 FROM Products_ProductComponents
	WHERE Products_ProductComponents.ComponentID  =  products_list_ORIG.derivedFrom
)

Open in new window

0

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
UnifiedISCommented:
"remove the PK, then delete dups then add PK back"
This wouldn't work, you can not add a key if it would violate the key you are adding.
0
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 2005

From novice to tech pro — start learning today.