Disclaimer: this change is not generally a useful thing to do to a properly normalized database, but I have business reasons for it.
I have a table of data with a primary key of numeric values. This key is used as a foreign key reference in multiple other tables. There is also a column of numeric values that can be updated to reflect the desired order for the rows. The order and PK columns contain the same numbers, but ordering the table by either column scrambles the other one.
What I'm trying to do is to update the primary key to follow the same order as the order column, but SSMS gives me the error "Violation of PRIMARY KEY constraint 'PK_Constraint'. Cannot insert duplicate key in object 'tbl'. The duplicate key value is <value>."
My update statement looks like this:
update tbl set tbl.key = tbl.order where tbl.key <> tbl.order
I already know how to update the foreign key references in the other tables, so I just need to know how I can update the key in this situation.