TSQL: UPDATE with INSERT INTO SELECT FROM

前端 未结 7 2221
感情败类
感情败类 2021-02-08 05:11

so I have an old database that I\'m migrating to a new one. The new one has a slightly different but mostly-compatible schema. Additionally, I want to renumber all tables from

7条回答
  •  清歌不尽
    2021-02-08 06:03

    The key with migration is to do several things: First, do not do anything without a current backup. Second, if the keys will be changing, you need to store both the old and new in the new structure at least temporarily (Permanently if the key field is exposed to the users because they may be searching by it to get old records).

    Next you need to have a thorough understanding of the relationships to child tables. If you change the key field all related tables must change as well. This is where having both old and new key stored comes in handy. If you forget to change any of them, the data will no longer be correct and will be useless. So this is a critical step.

    Pick out some test cases of particularly complex data making sure to include one or more test cases for each related table. Store the existing values in work tables.

    To start the migration you insert into the new table using a select from the old table. Depending on the amount of records, you may want to loop through batches (not one record at a time) to improve performance. If the new key is an identity, you simply put the value of the old key in its field and let the database create the new keys.

    Then do the same with the related tables. Then use the old key value in the table to update the foreign key fields with something like:

    Update t2
    set fkfield = newkey
    from table2 t2
    join table1 t1 on t1.oldkey = t2.fkfield
    

    Test your migration by running the test cases and comparing the data with what you stored from before the migration. It is utterly critical to thoroughly test migration data or you can't be sure the data is consistent with the old structure. Migration is a very complex action; it pays to take your time and do it very methodically and thoroughly.

提交回复
热议问题