C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » SQL Server »

how can i reset identity column in sql server to 1 programatically


Posted Date: 30 Jun 2008      Posted By: kamlesh      Member Level: Silver     Points: 1   Responses: 3



how can i reset identity column in sql server to 1 programatically




Responses

Author: www.DotNetVJ.com    30 Jun 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 6

Hi

Create one stored procedure for the below where stored procedure should accept table name and the value to start with.


dbcc checkident (EMP, reseed, 30)


Call the stored procedure from your application code.

Reseeding the identity value:

You can reseed the indentity value, that is, to have the identity values reset or start at a new predefined value by using DBCC CHECKIDENT. For example, if I have a table named MYTABLE and I want to reseed the indentity column to 30 I would execute the following:


dbcc checkident (EMP, reseed, 30)


If you wanted to reseed the table to start with an identity of 1 with the next insert then you would reseed the table's identity to 0. The identity seed is what the value is currently at, meaning that the next value will increment the seed and use that. However, one thing to keep in mind is that if you set the identity seed below values that you currently have in the table, that you will violate the indentity column's uniqueness constraint as soon as the values start to overlap. The identity value will not just “skip” values that already exist in the table.

Check out the below link
http://dotnetvj.blogspot.com/2008/01/identity-columns.html

Thanks -- Vj
http://dotnetvj.blogspot.com

Thanks -- Vijaya Kadiyala
http://www.DotNetVJ.com
Microsoft MVP
Me & My Little Techie



Author: Sandesh S    30 Jun 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

use the below query to reset identity column in sql server to 1
dbcc checkident (Table_name, reseed, 1)

Regards,
Sandesh Segu
http://sanssql.blogspot.com
http://www.orkut.co.in/Main#Community?cmm=96088888
SansSQL



Author: Sriram    01 Jul 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

create table pors(proid int identity(1,1),proname varchar(50))

select * from pors
insert into pors values('paste')

dbcc checkident (pors, reseed, 30)


create table pors(proid int identity(1,1),proname varchar(50))

select * from pors
insert into pors values('box')

dbcc checkident (pors, reseed, 30)

set identity_insert pors off
delete pors where proname='gox'
insert into pors(proid,proname) values(36,'sdfds')



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : How to find the database size in live server
Previous : How to Create a #TEMP table dynamically
Return to Discussion Forum
Post New Message
Category: SQL Server

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use