Wednesday, March 28, 2012

Script to reset the "Identity Seed" and "Identity Increment" values

hi,

Can any one help me out with the script to reset the "Identity Seed" and "Identity Increment" values for the Identity field.

I can go to enterprise manager and do this or event Generate the script. The script Enterprise manager is creating drops the original table and recreates the table with new defination.

The table can't be droped as it is part of replication.
Is there a work around for this ?
Can I change the "Identity Seed" and "Identity Increment" fields with out droping the table. ?

Any help whould be greatly appreciated.

Thanks,
Prashanth ReddyOriginally posted by Prashanth
hi,

Can any one help me out with the script to reset the "Identity Seed" and "Identity Increment" values for the Identity field.

I can go to enterprise manager and do this or event Generate the script. The script Enterprise manager is creating drops the original table and recreates the table with new defination.

The table can't be droped as it is part of replication.
Is there a work around for this ?
Can I change the "Identity Seed" and "Identity Increment" fields with out droping the table. ?

Any help whould be greatly appreciated.

Thanks,
Prashanth Reddy

You have 2 options that spring to mind:

1. You can use TRUNCATE TABLE to accomplish this, but you will lose all data and as its a non-logged operation nothing will be written to the transaction log. I should add that if the table is referenced by a foriegn key constraint then you will not be able to use this method.

Usage: TRUNCATE TABLE tablename

2. The other option is to use DBCC CHECKIDENT to reset the seed:

Usage: DBCC CHECKIDENT (tablename, RESEED, 0)

macka.|||thanks for the response.|||thanks for the response.

I have patient table with PatientID as auto increment field
with
"Identity Increment" as 1 and
"Identity seed" as 1
I can chane Identity Seed using
DBCC checkident (patient,reseed,4)
How do i change the "Identity Increment" value to 10?

Prashanth|||The following script should work. In this example it resets the seed to 4 and increment to 10.

If the table is referenced by foreign key constraints then you will need to drop them before this script is run and add them back after it completes.

ALTER TABLE Demo
DROP CONSTRAINT PK_Demo

ALTER TABLE Demo
DROP COLUMN DemoID

ALTER TABLE Demo
ADD DemoID INT IDENTITY(4,10)

ALTER TABLE Demo ADD CONSTRAINT
PK_Demo PRIMARY KEY CLUSTERED
(
DemoID
) ON [PRIMARY]

macka.

No comments:

Post a Comment