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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s