23

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 the highest ID plus 1 if the most recent row was deleted.

I ask this because we occassionally have to update a live DB with new rows. The ID of the row is used in our code (e.g. Switch (ID){ Case ID: } and must be the same. If our development DB and live DB get out of sync, it would be nice to predict a row ID in advance before deployment.

I could of course SET IDENTITY OFF SET INSERT_IDENTITY ON or run a transaction (does this roll back the ID?) etc but wondered if there was a function that returned the next ID (without incrementing it).

2
  • A transaction will not rollback the identity counter and there is allways the risk that some other thread uses the number you think you will get later on.
    – idstam
    Commented Oct 9, 2009 at 11:21
  • Thanks for this @idstam I did suspect this.
    – iWeasel
    Commented Oct 9, 2009 at 14:06

5 Answers 5

20

try IDENT_CURRENT:

Select IDENT_CURRENT('yourtablename')

This works even if you haven't inserted any rows in the current session:

Returns the last identity value generated for a specified table or view. The last identity value generated can be for any session and any scope.

1
  • 2
    Nice. This gets the most recent value used, and the MSDN page alerts that "Be cautious about using IDENT_CURRENT to predict the next generated identity value. The actual generated value may be different from IDENT_CURRENT plus IDENTITY_SEED because of insertions performed by other sessions."
    – PaulG
    Commented Oct 9, 2009 at 11:27
16

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.

4
  • Yikes! +1 for an answer that made me smile
    – iWeasel
    Commented Oct 9, 2009 at 13:56
  • I've spent most of the night researching it, thinking it would make for a decent blog item, and figured it out. Editing my answer.
    – Andrew
    Commented Oct 9, 2009 at 21:21
  • @Andrew. The thought that you have too much time on your hands did occur to me. On the other hand a big thanks. Great answer.
    – iWeasel
    Commented Oct 12, 2009 at 12:16
  • nice! I wonder if locking the table would make it safe to use?
    – callisto
    Commented Sep 15, 2011 at 9:30
8

This is a little bit strange but it will work:

If you want to know the next value, start by getting the greatest value plus one:

SELECT max(id) FROM yourtable

To make this work, you'll need to reset the identity on insert:

DECLARE @value INTEGER

SELECT @value = max(id) + 1 FROM yourtable

DBCC CHECKIDENT (yourtable, reseed, @value)

INSERT INTO yourtable ...

Not exactly an elegant solution but I haven't had my coffee yet ;-)

(This also assumes that there is nothing done to the table by your process or any other process between the first and second blocks of code).

2
  • This may not be elegant but I will mark this as the answer for tables where you can be fairly certain that no new row is inserted before you run this.
    – iWeasel
    Commented Oct 9, 2009 at 14:09
  • Sorry Jeff, @Andrew spent all night on an excellent edit. Works a treat. Gonna have to re-assign answer.
    – iWeasel
    Commented Oct 12, 2009 at 12:15
4

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

3
  • 2
    Precisely; there is no guarantee, which is why I wondered if there was an alternative. :)
    – iWeasel
    Commented Oct 9, 2009 at 10:37
  • 1
    No, there is no other way - SQL Server will only ever really issue the ID when you actually do the INSERT - no way of faking that :-(
    – marc_s
    Commented Oct 9, 2009 at 10:40
  • 1
    But using ident_incr will tell you the increment... better than assuming +1
    – defines
    Commented May 10, 2018 at 17:18
3

Rather than using an IDENTITY column, you could use a UNIQUEIDENTIFIER (Guid) column as the unique row identifer and insert known values.

The other option (which I use) is SET IDENTITY_INSERT ON, where the row IDs are managed in a source controlled single 'document'.

7
  • 1
    Excellent point, but Guids are not really practical on this particular table and are a bit space hungry. I do use IDENTITY_INSERT at the moment. It works but wonder if there is an alternative. :)
    – iWeasel
    Commented Oct 9, 2009 at 10:40
  • +1 for Guids. Guids do have a performance penalty relative to an auto-increment column (although the extra space they require in a row is trivial), but they totally eliminate the need to jump through hoops like this. Commented Oct 9, 2009 at 12:03
  • The penalty is if you use the GUID as your clustered key, then you pay a heavy price both in terms of performance and space.
    – Andrew
    Commented Oct 9, 2009 at 15:04
  • @Andrew : that's not entirely true performance-wise, if you have a regular index rebuild. Space, well it's 16 bytes per row as oppose to 4 bytes per row Commented Oct 9, 2009 at 15:23
  • If it is the clustered key, then the key reappears in each additional NC index on the table, as well as causing a high volume of page splits. If it's the clustered key, you pay a price on both the storage and space.
    – Andrew
    Commented Oct 9, 2009 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.