Monday, December 31, 2007

Reset an identity column of a table in SQL Server

This script came in handy when I was trying to help my client move from development to production server.

DBCC CHECKIDENT(table_name, RESEED, 0).

The table_name is the parameter that you would need to change per your table name. Also, this may be a good candidate to have a front end for the administrator to do maintenance in the future. Although, if you have a DBA he would rather prefer to execute this script from SQL management studio.

1 comment:

Srinivasan Prasanna said...

I got this information on Oracle from my friend Ram.

In oracle, there is no concept of identity column.
There is a pseudo column called ROWNUM and the value for this returned as you run the query.
Another thing is you can create a sequence which can be incremented and assign the sequence value to a column in a table. The sequence is created using CREATE SEQUENCE command

Creating a Sequence: Example

The following statement creates the sequence customers_seq in the sample schema oe. This sequence could be used to provide customer ID numbers when rows are added to the customers table.

CREATE SEQUENCE customers_seq
START WITH 1000
INCREMENT BY 1
NOCACHE
NOCYCLE;

The first reference to customers_seq.nextval returns 1000. The second returns 1001. Each subsequent reference will return a value 1 greater than the previous reference.