SQL Server Hidden Gem - How to get next value of Identity for a column in a table


This is very nice tip if you ever wanted to find the next value of identity column without doing an insert, I repeat without doing an Insert.

Lets say you have a table named company. Then you would use this command

SELECT IDENT_CURRENT('company') + IDENT_INCR('company')

where IDENT_CURRENT gives the current or last identity value used by that table and IDENT_INCR gives the increment you/anyone had set during creation of table. It could 1, 2 etc depending on your logic.

The other way of finding next value of identity is after an insert statement by using SCOPE_IDENTITY(). You would add 1 or 2 and so on depending on the autoincrement setting you used during the table creation.

Hope this helps.


Popular posts from this blog

Making text bold in Adobe Acrobat

Java Keytool Export Private Key, PKCS#12 or .p12 export or conversion from java keystore

iphone 4 ipod touch Change Delete Edit Update Delete wifi wireless key wireless password wireless connection