Does SQL Server guarantee sequential inserting of an identity column?

后端 未结 5 1211
情话喂你
情话喂你 2021-02-13 15:12

In other words, is the following \"cursoring\" approach guaranteed to work:

  1. retrieve rows from DB
  2. save the largest ID from the returned records for later,
5条回答
  •  梦谈多话
    2021-02-13 15:46

    Identities will will always follow the increment that defines the identity:

    IDENTITY [(seed ,increment)] http://msdn.microsoft.com/en-us/library/aa933196(SQL.80).aspx

    which can be positive or negative (you can have it increment forward or backwards). If you set your identity to increment forward, your identity values will always be larger than the previous, but you may miss some, if you rollback an INSERT.

    Yes, if you set your identity increment to a positive value your loop logic will work.

提交回复
热议问题