I'm inserting new records into a Person table, and if there's already a record with the same SSN, I want to backup this old record to another table (let's call it PersonsBackup) and update the row with my new values. There is an identity column in Person table that serves as my primary key, which has to be the same.
Source table structure:
Name | Addr | SSN
Person table structure:
PrimaryKeyID | Name | Addr | SSN
PersonBackup table structure:
BackupKeyID | Name | Addr | SSN | OriginalPrimaryKeyID
where OriginalPrimaryKeyID = PrimaryKeyID for the record that was backed up. How can this be done? I was thinking of using cursor to check if SSN matches, then insert that record accordingly, but I've been told that using cursors like this is very inefficient. Thanks for your help!
MERGEstatement, it can do the job: msdn.microsoft.com/en-us/library/bb510625.aspxMERGEdoesn't support two target tables, does it?