How to get the next identity value from SQL Server

后端 未结 5 669
悲哀的现实
悲哀的现实 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 05:11

    In case when your table will be empty then this query will work perfectly.

    SELECT
      CASE
        WHEN (SELECT
            COUNT(1)
          FROM tablename) = 0 THEN 1
        ELSE IDENT_CURRENT('tablename') + 1
      END AS Current_Identity;
    

提交回复
热议问题