TSQL: UPDATE with INSERT INTO SELECT FROM

前端 未结 7 2231
感情败类
感情败类 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:09

    Is there a way to do this UPDATE via INSERT INTO SELECT FROM so I don't have to process every record manually?

    Since you wouldn't want to do it manually, but automatically, create a trigger on MV6.Posts so that UPDATE occurs on MV5.Posts automatically when you insert into MV6.Posts.

    And your trigger might look something like,

    create trigger trg_MV6Posts
    on MV6.Posts
    after insert
    as
    begin
        set identity_insert MV5.Posts on
    
        update  MV5.Posts
        set ID = I.ID
        from    inserted I
    
        set identity_insert MV5.Posts off
    end
    

提交回复
热议问题