Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Browse by Tags · View All
XML 112
TSQL 69
XQuery 69
XQuery Functions 67
XQuery Training 65
XQuery in TSQL 64
XQuery Tutorial 63
SQL Server XQuery 63
XQuery-Labs 57
BRH 38

Archive · View All
September 2008 32
August 2008 30
July 2008 21
August 2009 19
June 2009 19
May 2010 18
January 2009 15
January 2010 14
October 2008 14
June 2008 13

SQL Server - How to find out the SEED and INCREMENT values of an IDENTITY column?

Feb 3 2009 12:32AM by Jacob Sebastian   

Every IDENTITY column has two basic attributes: SEED and INCREMENT. The SEED attribute stores the ROOT or Starting value. INCREMENT specifies the value to be added to the current IDENTITY value to generate the next value. The default value of both these attributes is 1. So unless you specify a different value, an IDENTITY column starts with 1 and increments by 1.

How to find the SEED value of an IDENTITY column?

You can use the system function IDENT_SEED() to retrieve the SEED value of an IDENTITY column. for example:

IF OBJECT_ID('Employees','U') IS NOT NULL
    DROP TABLE Employees
    
CREATE TABLE Employees (EmpID INT IDENTITY, Name VARCHAR(20))
GO

SELECT IDENT_SEED('Employees') AS IdentitySeed
/*
IdentitySeed
---------------------------------------
1
*/

How to find the INCREMENT value of an IDENTITY column?

Just like IDENT_SEED(), there is another function that returns the INCREMENT value of an IDENTITY column: IDENT_INCR(). The following example demonstrates that.

IF OBJECT_ID('Employees','U') IS NOT NULL
    DROP TABLE Employees
    
CREATE TABLE Employees (EmpID INT IDENTITY, Name VARCHAR(20))
GO

SELECT IDENT_INCR('Employees') AS IdentityIncrement
/*
IdentityIncrement
---------------------------------------
1
*/

How to find the current IDENTITY value of a table?

The system function IDENT_CURRENT() returns the last generated IDENTITY value of a table. If the table is empty (no records generated yet or truncated), this function will return the IDENTITY SEED. Let us look at an example:

IF OBJECT_ID('Employees','U') IS NOT NULL
    DROP TABLE Employees
    
CREATE TABLE Employees (EmpID INT IDENTITY, Name VARCHAR(20))
GO

SELECT IDENT_CURRENT('Employees') AS LastIdentityValue
-- returns 1

INSERT INTO Employees(Name) SELECT 'Jacob'
SELECT IDENT_CURRENT('Employees') AS LastIdentityValue
-- returns 1

INSERT INTO Employees(Name) SELECT 'Steve'
SELECT IDENT_CURRENT('Employees') AS LastIdentityValue
-- returns 2

SQL Server 2005 Books Online documentation incorrectly states that the function returns NULL if called for a table that is empty (no records inserted yet or the table is truncated). The above example shows that in such case, SQL Server returns the IDENTITY SEED value, instead of NULL value. This documentation error is corrected in SQL Server 2008 Books Online Documentation.

Retrieve the SEED, INCREMENT and LAST IDENTITY value of all tables in a database

Let us try to write a query that returns the SEED, INCREMENT and LAST IDENTITY value of all tables in the AdventureWorks database. Here is the query:

WITH cte AS (
    SELECT
        s.name + '.' + t.name AS TableName,
        c.name AS ColumnName
    FROM sys.tables t
    INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
    INNER JOIN sys.columns c ON c.object_id = t.object_id
        AND c.is_identity = 1
)
SELECT 
    TableName,
    ColumnName,
    IDENT_SEED(TableName) AS Seed,
    IDENT_INCR(TableName) AS Increment,
    IDENT_CURRENT(TableName) AS LastIdentity
FROM cte
/*
TableName                      ColumnName                Seed        Increment   LastIdentity
------------------------------ ------------------------- ----------- ----------- ------------
Person.Address                 AddressID                 1           1           32521
Production.ProductReview       ProductReviewID           1           1           4
Production.TransactionHistory  TransactionID             100000      1           213442
Person.AddressType             AddressTypeID             1           1           6
Production.ProductSubcategory  ProductSubcategoryID      1           1           37
...
...
*/

Question to Readers: Can you think of a scenario where you need to know the SEED, INCREMENT or CURRENT IDENTITY value of a table?

Tags: IDENTITY, TSQL, SQL SERVER, IDENT_CURRENT, SEED, INCREMENT, IDENT_INCR, IDENT_SEED, sys.tables, sys.columns, sys.schemas, identity seed, identity increment, current-identity,


Jacob Sebastian
1 · 100% · 32004
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"SQL Server - How to find out the SEED and INCREMENT values of an IDENTITY column?" rated 5 out of 5 by 1 readers
SQL Server - How to find out the SEED and INCREMENT values of an IDENTITY column? , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]