I’m trying to update a field in SQL Server 2005 from one table to another field in another table, and I’m getting this whole violation of UNIQUE KEY constraint and I can’t seem to figure out where it went wrong with my script. Here’s what I have so far:
UPDATE table1
SET column1 = (SELECT table2.column2
FROM table2 INNER JOIN
table1 ON table1.id = table2.id)
I’m quite new to SQL and would definitely love some input in regard to this situation. Thank you so much!
This syntax seems a little cleaner, but you will need to validate that the values in table2.column2 are unique.
Assuming table1.column1 has a unique constraint, and that id is the primary key in each table, you can check for potential violations first. This will tell you if there are dupes in column2 in the source table:
And this will tell you if there are single values in table2 that will conflict with other rows that aren’t common between the two tables: