Getting the next ID without inserting a row

前端 未结 5 791
臣服心动
臣服心动 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 11:17

    Edit:

    After spending a number of hours comparing entire page dumps, I realised there is an easier way and I should of stayed on the DMVs.

    The value survives a backup / restore, which is a clear indication that it is stored - I dumped all the pages in the DB and couldn't find the location / alteration for when a record was added. Comparing 200k line dumps of pages isn't fun.

    I had used the dedicated admin console I took a dump of every single internal table exposed inserted a row and then took a further dump of the system tables. Both of the dumps were identical, which indicates that whilst it survived, and therefore must be stored, it is not exposed even at that level.

    So after going around in a circle I realised the DMV did have the answer.

    create table foo (MyID int identity not null, MyField char(10))
    insert into foo values ('test')
    go 10
    
    -- Inserted 10 rows
    select Convert(varchar(8),increment_value) as IncrementValue,
       Convert(varchar(8),last_value) as LastValue
    from sys.identity_columns where name ='myid'
    
    
    -- insert another row
    insert into foo values ('test')
    
    -- check the values again
    select Convert(varchar(8),increment_value) as IncrementValue,
       Convert(varchar(8),last_value) as LastValue
    from sys.identity_columns where name ='myid'
    
    -- delete the rows
    delete from foo
    
    
    -- check the DMV again
    select Convert(varchar(8),increment_value) as IncrementValue,
       Convert(varchar(8),last_value) as LastValue
    from sys.identity_columns where name ='myid'
    
    -- value is currently 11 and increment is 1, so the next insert gets 12
    insert into foo values ('test')
    select * from foo
    
    Result:
    MyID        MyField
    ----------- ----------
    12          test      
    
    (1 row(s) affected)
    

    Just because the rows got removed, the last value was not reset, so the last value + increment should be the right answer.

    Also going to write up the episode on my blog.

    Oh, and the short cut to it all:

    select ident_current('foo') + ident_incr('foo')
    

    So it actually turns out to be easy - but this all assumes no one else has used your ID whilst you got it back. Fine for investigation, but I wouldn't want to use it in code.

提交回复
热议问题