Checking current IDENTITY value

While SQL Server does a good job managing numbers for IDENTITY columns, a DBA may need to take a look at the current state of them. One of the questions that might be asked is what is the current IDENTITY value? That question can be asked to verify whether the value is reaching a maximum value allowed by the column datatype or to confirm IDENTITY values are aligned with maximum column values after deleting many rows.

There are a few ways of getting current IDENTITY values. Having options is very good because not all of them can be applied in all cases because of user permissions.

DBCC command

Using DBCC command is one of the options:

dbcc checkident ('dbo.Products', noreseed)
Checking identity information: current identity value '77', current column value '77'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

The command returns the current IDENTITY value and a maximum value of the column values.

 

ident_current function

Another option is ident_current function. Advantage of it is a possibility of using it in a SQL query.

select ident_current('dbo.Products')

oneIdentityValues

 

ident_current can be easily used to get all identity values for all tables using a simple query:

select table_schema schemaName, table_name tableName, ident_current(table_schema + '.' + table_name) identValue
from information_schema.tables
where objectproperty(object_id(table_schema + '.' + table_name), 'TableHasIdentity') = 1

allIdentityValues

 

 

We use cookies

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.