[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2008-11-06
2
Medium Priority
?
200 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:supergirl2008
2 Comments
 
LVL 9

Accepted Solution

by:
Ernariash earned 1500 total points
ID: 22898416
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
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 22898631
"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

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question