TSQL: UPDATE with INSERT INTO SELECT FROM

前端 未结 7 2219
感情败类
感情败类 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 05:45

    Probably the simplest way would be to add a column on MV6.Posts for oldId, then insert all the records from the old table into the new table. Last, update the old table matching on oldId in the new table with something like:

    UPDATE mv5.posts
    SET newid = n.id
    FROM mv5.posts o, mv6.posts n 
    WHERE o.id = n.oldid
    

    You could clean up and drop the oldId column afterwards if you wanted to.

提交回复
热议问题