Friday 7 October 2011

How to reset the identity column

Generally when we create a table and set identity and insert the data for testing purpose  but when we delete the data and feed original data then identity start with last not start with 1.
Here is solution
lets we create the table

create table ABC
(
id int identity(1,1),
name varchar(30)
)

and insert some data

insert into abc values ('ROHIT')
insert into abc values ('Riya')
insert into abc values ('Ankita')
insert into abc values('Kumar')
insert into abc values('baby')

and then select the data then output is like below


Select * from abc


1 ROHIT
2 Riya
3 Priya
4 Kumar
5 baby

but when we delete data from table abc and after that insert some data then it started with 6 id not 1
so to achieve this we use following query
after the delete command
delete from abc
we use the below query
DBCC CHECKIDENT (abc, RESEED, 0)
note : where abc is the table name
after this when we start enter the data then id start with 1 not 6

Hope this will help you if any problem then please let me know

Thanks
ROHIT SRIVASTAVA


No comments:

Post a Comment