First, changing the primary key value is never a good idea. The focus should be on trying to avoid it by all means.
If you cannot eliminate the need to update the primary key value, the best choice would be to determine the foreign key relationship between the two tables using ON UPDATE CASCADE , so that any changes to the primary key of the table will automatically be cascaded to the child table.
To do this, undo the existing foreign key ratio, and then add:
ALTER TABLE dbo.Person2Address ADD CONSTRAINT FK_Person2Address_Person FOREIGN KEY (PersonId) REFERENCES dbo.Person(Id) ON UPDATE CASCADE
Then, the value of Person2Address table PersonId automatically updated if the Id on the person changes.
Now you can just call
UPDATE dbo.Person SET Id = NewId WHERE Id = OldId
and that should be all there is!
marc_s
source share