Getting the next value of the primary key from MSSQL table

SELECT IDENT_CURRENT(‘<Table Name>‘);

SELECT IDENT_CURRENT(‘<Table Name>‘)+IDENT_INCR(‘<Table Name>‘);

SELECT MAX(<primary key column name>) FROM <Table Name>;

This won’t work if the database is empty, because it returns NULL.

⇒ Please note that this method can be used to predict the next id, but does not guarantee this return value. Because the time you execute request and time it take to display result, another transaction occurred, making return value null and void.

IDENT_CURRENT (Transact-SQL)

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.

Advertisements