Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Fixing Your Identity

By Steve Jones,

Fixing Your Identity

Introduction

I used identity fields quite a bit in my schemas. Whether or not this is a good idea, I won't debate in this article, but I often find the need to generate some artificial id for a table that doesn't have any apparent primary key.

One of the things that we occassionaly find is that developers have a need to hard code an identity value for particular code triggers. They do this for various reasons, but it usually means that we need to ensure that the identity values in development and production evironments remain in synch.

Occassionally, our testing gets a little out of synch with the release of new code to the live encivironment and the identity values are not the same. When this happens, I have to "reset" the identity value to synch back up the two databases. If anyone else out there uses identity values, you have probably run into the need to change the functionality of the identity property.

I was expecting that I would have to manually hack some system tables to fix my systems, but was pleasantly surprised. After a little research in Books Online, I discovered that the SQL Server development team anticipated this and provided a utility to handle these situations. There is a DBCC utility that takes the following form:

dbcc checkident('<table>, <NORESEED | RESEED>, <value>)
whose parameters are defined as follows:
  • table - This is the name of the table that has the identity column. This name must be quoted or listed like any other identifier.
  • NORESEED - This flag implues that the identity value should not be changed.
  • RESEED - This flag implues that the identity value should be changed. Kind of obvious, but that's what's in BOL.
  • value - This is an optional value that can be included and the table will take this as the new identity value. This means the next row will use this value if there ARE NOT ROWS in the table. Otherwise the next row will use this value + 1.
Using this utility, you can find out information about the next value that will be used or reset the value to a new value. For my situation, I needed to delete data rows, reset the identity value to a lower seed, and then reinsert these values in the correct order. I deleted the data and then ran
	    DBCC CHECKIDENT (marketing, RESEED, 55)
	

Conclusions

Working with identity values is like most operations in SQL Server, there are positive and negative points and you must make an intelligent decision based on the situation. I hope this article has shown that there is some control when using identity values. While you should not use this too extensively, IMHO, to manage the values that the server generates, this allows you to correct mistakes or manage the value if you have the need. Be sure if you use this that you understand the next value that will be used.

As always I welcome feedback on this article using the "Your Opinion" button below. Please also rate this article. Please don't comment on whether identity values are good or bad in this space as this article is merely concerned with altering identity values that do exist.

Steve Jones
©dkRanch.net November 2001
Return to Steve Jones Home

 

Total article views: 7088 | Views in the last 30 days: 4
 
Related Articles
ARTICLE

Changing a Non-IDENTITY column to IDENTITY and vice versa

This article from new author Thomas Pieries, brings us a few methods for changing a Non-IDENTITY col...

FORUM

query to change the field identity,Identity seed,Identity Increment

query to change the field identity,Identity seed,Identity Increment

FORUM

Change in Publication articles in Relication

Change in Publication articles in Relication

FORUM

Identity column value should not be increased if there's pbm in insertion of record.

Identity column value should not be increased if there's pbm in insertion of record.

ARTICLE

SQL Server 2005 for SQL2k Developer (Part 1)

There are many changes in SQL Server 2005, especially for the SQL Server developer. New options, fea...

Tags
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones