Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 resetting the identity column values

Author  Topic 

edyl
Starting Member

35 Posts

Posted - 2011-08-04 : 17:07:51
Hello Everyone,

I am populating a table in increments which has a Primary Key as an Identity Column. The table currently has about 400K rows. The rows that were inserted after 200100th row had some problems and I deleted them in bulks. I deleted about 100K rows and then picked up the insertion again with good data. However when the good data were inserted I noticed that the count values for identity PK did not pick up from where it was left. So the identity PK value for the next row after 200100th row instead of being 200101 is now 300050. I believe that is because the identity counter is still accommodating for 100K some rows that were deleted earlier.

Without noticing it sooner, I went on inserting another 200K rows of good data. The table now has about 400K rows but the PK value for the last row in the table is 550120. Is there a way to readjust the PK values for the rows after the 200100th value so the count of row s in table and the identity PK value for the last row correspond? Is there a way to prevent this from happening in the future?

Any suggestion, any recommendation greatly appreciated.

Thanks in Advance.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-04 : 17:13:40
Why does this gap matter? If you don't want gaps due to deletes, then you can't use the identity value. Or don't delete data, mark the row for deletion instead.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

edyl
Starting Member

35 Posts

Posted - 2011-08-05 : 09:37:54
Thanks.

quote:
don't delete data, mark the row for deletion instead


Are you indicating to use some sort of delete flag?

Regards.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-05 : 13:59:26
Yes, you would have a column that indicated if this row is no longer needed. Then your application would ignore those rows. I am not necessarily suggesting you do this, but it would eliminate your gaps issue.

The best solution is to delete your data and ignore the identity gaps.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

edyl
Starting Member

35 Posts

Posted - 2011-08-05 : 16:21:40
Thanks for your insights. I am ignoring the gaps and hoping the DBAs won't scratch their heads...:-D.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-08-05 : 16:35:57
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -