Link to home
Start Free TrialLog in
Avatar of supergirl2008
supergirl2008

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Ernariash
Ernariash
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of UnifiedIS
UnifiedIS

"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.