Filling the gaps in values of IDENTITY column

后端 未结 4 1743
南旧
南旧 2020-12-19 20:15

I have a table with an IDENTITY column

[Id] int IDENTITY(1, 1) NOT NULL

After some rows beeing added/removed I end with gaps in Id values:<

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 20:51

    I cannot control the part which requires ID columns to be in sequence.

    This sounds like there is program logic which assumes there are no gaps--correct?

    I need this to keep two different databases in sync.

    It's still not clear what you mean. If the actual values in the IDENTITY column are not meaningful (not used as foreign keys by other tables), you can just do this:

    DELETE FROM db1.table
    SELECT col1, col2, col3 /* leave out the IDENTITY column */ 
    INTO db1.table FROM db2.table
    

提交回复
热议问题