LP on .NET

January 31, 2012

Obtaining SQL Identity Information

Filed under: Software Development,SQL Server — Larry Parker @ 7:05 pm

I found a great query today on this blog for obtaining information about SQL identity columns.  Here’s my updated version to handle SQL schemas:

SELECT IDENT_SEED(TABLE_SCHEMA + '.' + TABLE_NAME) AS Seed,
    IDENT_INCR(TABLE_SCHEMA + '.' + TABLE_NAME) AS Increment,
    IDENT_CURRENT(TABLE_SCHEMA + '.' + TABLE_NAME) AS Current_Identity,
    TABLE_SCHEMA + '.' + TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE OBJECTPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), 'TableHasIdentity') = 1
  AND TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_SCHEMA + '.' + TABLE_NAME

This allows you to easily find out every table in your database that has an identity column, and what the seed, increment, and current values are.

Super helpful!

Advertisements

Blog at WordPress.com.