How to get the next identity value from SQL Server

后端 未结 5 664
悲哀的现实
悲哀的现实 2020-12-06 04:08

I need to get the next identity value from SQL Server.

I use this code :

SELECT IDENT_CURRENT(\'table_name\') + 1

This

5条回答
  •  情书的邮戳
    2020-12-06 04:48

    I think you'll want to look for an alternative way to calculate the next available value (such as setting the column to auto-increment).

    From the IDENT_CURRENT documentation, regarding empty tables:

    When the IDENT_CURRENT value is NULL (because the table has never contained rows or has been truncated), the IDENT_CURRENT function returns the seed value.

    It doesn't even seem all that reliable, especially if you end up designing an app that has more than one person writing to the table at the same time.

    Be cautious about using IDENT_CURRENT to predict the next generated identity value. The actual generated value may be different from IDENT_CURRENT plus IDENT_INCR because of insertions performed by other sessions.

提交回复
热议问题