Getting the next ID without inserting a row

前端 未结 5 781
臣服心动
臣服心动 2021-02-05 10:50

Is it possible in SQL (SQL Server) to retrieve the next ID (integer) from an identity column in a table before, and without actually, inserting a row? This is not necessarily th

5条回答
  •  后悔当初
    2021-02-05 10:55

    You can pretty easily determine that the last value used is:

    SELECT
        last_value
    FROM 
        sys.identity_columns
    WHERE
        object_id = OBJECT_ID('yourtablename')
    

    Usually, the next ID will be last_value + 1 - but there's no guarantee for that.

    Marc

提交回复
热议问题