Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

How to reset Identity of Autonumber column?

Total Hit ( 4959)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


There are two ways to reset identity field

1) Use TRUNCATE TABLE rather than DELETE. This is a non-logged operation, however, and may not be what you need.

2) Use the DBCC CHECKIDENT command to reseed your identity value:

DBCC CHECKIDENT('mytable', RESEED, 0) -- next row inserted will use IDENTITY value 1

following example will clerify more.

Click here to copy the following block
if (select object_id('#a') ) IS NOT NULL
   drop table #a

declare @new_reseed_value int

create table #a
(f1 int IDENTITY NOT NULL,
f2 varchar(50))

INSERT INTO #a VALUES ('AAA')
INSERT INTO #a VALUES ('BBB')
INSERT INTO #a VALUES ('CCC')
INSERT INTO #a VALUES ('DDD')
INSERT INTO #a VALUES ('EEE')
INSERT INTO #a VALUES ('FFF')

select * from #a --//

delete from #a where f1>=5

INSERT INTO #a VALUES ('EEE')
INSERT INTO #a VALUES ('FFF')

select * from #a --//insert without reset

delete from #a where f1>=5

set @new_reseed_value=(select max(f1) from #a)
DBCC CHECKIDENT (#a, RESEED,@new_reseed_value)

INSERT INTO #a VALUES ('EEE')
INSERT INTO #a VALUES ('FFF')

select * from #a --//insert with reset


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.