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 - IDENTITY - How to change/reset the IDENTITY values?

Jan 30 2009 12:05AM by Jacob Sebastian   

In the previous post, we saw that the IDENTITY values can go out of sequence in several cases. Some times, you might decide to delete all rows from a table, and start filling fresh data. At this time, you might prefer to reset the IDENTITY value to start a  new sequence.

Run the following script to create a sample table.

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

INSERT INTO Employees (Name) SELECT 'Jacob'
INSERT INTO Employees (Name) SELECT 'Steve'

SELECT * FROM Employees
/*
EmpID       Name
----------- --------------------
1           Jacob
2           Steve
*/

Let us now delete all rows from this table and insert  a new row.

DELETE FROM Employees
INSERT INTO Employees (Name) SELECT 'Jacob'
SELECT * FROM Employees
/*
EmpID       Name
----------- --------------------
3           Jacob
*/

As we figured earlier, the IDENTITY value started with 3, not with 1. Let us now see how we can reset the IDENTITY value back to 1.

TRUNCATE TABLE resets IDENTITY values

When you truncate a table, the IDENTITY value will be RESET. Here is an example:

TRUNCATE TABLE Employees
INSERT INTO Employees (Name) SELECT 'Jacob'
SELECT * FROM Employees
/*
EmpID       Name
----------- --------------------
1           Jacob
*/

Note that the new IDENTITY value started from 1.

DBCC CHECKIDENT

You can use DBCC CHECKIDENT to reset the IDENTITY seed. Before we run the example, let us rebuild the table again.

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

INSERT INTO Employees (Name) SELECT 'Jacob'
INSERT INTO Employees (Name) SELECT 'Steve'

SELECT * FROM Employees
/*
EmpID       Name
----------- --------------------
1           Jacob
2           Steve
*/

Now, let us try DBCC CHECKIDENT

DELETE FROM Employees
DBCC CHECKIDENT('Employees',RESEED, 0)
INSERT INTO Employees (Name) SELECT 'Jacob'
SELECT * FROM Employees
/*
EmpID       Name
----------- --------------------
1           Jacob
*/

It is also possible to change the NEXT identity value to a value of your choice. The following example changes the current identity value to be 99, so that the next value generated will be 100.

DBCC CHECKIDENT('Employees',RESEED, 99)
INSERT INTO Employees (Name) SELECT 'Bob'
SELECT * FROM Employees
/*
EmpID       Name
----------- --------------------
1           Jacob
100         Bob
*/

Question to Readers: Did you ever come across a requirement for resetting the IDENTITY value? If so, which method did you use?

Tags: IDENTITY, TSQL, SQL SERVER, SQL, TRUNCATE TABLE, RESET IDENTITY, DBCC RESEED, CHANGE_IDENTITY, DBCC CHECKIDENT,


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



Submit

2  Comments  

  • i like your posts. better crisp clarity than other posts. thanks for all the topics and making everyone understand the nuances of sql server and how to do things in correct way. really appreciate it. if you ever visit india or do any session in india, i would like to attend it. Thanks again

    commented on Jul 7 2011 10:30PM
    amit
    2903 · 0% · 2
  • @Amit,

    I am glad to know that you found the posts helpful. I live in Ahmedabad and usually present in Tech-ED India as well as Community Techdays Ahmedabad. So looking forward to see you some day :-)

    commented on Jul 8 2011 8:34AM
    Jacob Sebastian
    1 · 100% · 32004

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]