question

Slick84 avatar image
Slick84 asked

Identity Value reseed did not reseed!

Hi,

I have a small problem. I was rolling out new changes to our production database. This rollout required me to delete a few tables entirely, reseed them with a "zero" value and when the tables would get repopulated, the identity values would restart from zero. However, that did not happen all my tables have different identity values (depending on what was the last identity value before they were entirely deleted). For example, table A has the first identity value of 6437 and it continues sequentially.

Table B for example starts with 335 identity value now and so on.

What didnt my reseed script work? Please see below my code for deleting & reseeding.

DELETE FROM [Database].[dbo].[TableAAAAAA]
DBCC CHECKIDENT ([TableAAAAAA], reseed, 0)

DELETE FROM [Database].[dbo].[TableBBBBB]
DBCC CHECKIDENT ([TableBBBBB], reseed, 0)

DELETE FROM [Database].[dbo].[TableCCCCC]
DBCC CHECKIDENT ([TableCCCCC], reseed, 0)

Using SQL Server 2005 Standard Edition.

Thanks, S

sql-server-2005t-sqldbccidentity
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Blackhawk-17 avatar image
Blackhawk-17 answered

I would suggest TRUNCATE table rather then DELETE.

I beleive it is a flaw/limitation in DBCC CHECKIDENT.


I think this is ourr explanation...

From Books Online:

Current identity value is set to the new_reseed_value. If no rows have been inserted to the table since it was created, or all rows have been removed by using the TRUNCATE TABLE statement, the first row inserted after you run DBCC CHECKIDENT uses new_reseed_value as the identity. Otherwise, the next row inserted uses new_reseed_value + the current increment value.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered

Just a thought - Did you run those statements in the correct database?

You may have deleted from database 'Database', but the reseed would be run in the current database - which may be different!

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I made a script and used the "USE DATABASE" statement on top. Good catch though because I thought about this too.
0 Likes 0 ·
Jay Bonk avatar image
Jay Bonk answered

I don't think that the issue has to do with delete v. truncate. I created the following test script, which can't use the truncate due to the FK constraint to mimic the OP.

I ran this, it worked and reseeded the table correctly:

SET NOCOUNT ON;
GO

CREATE TABLE Test1 
(
    ID INT IDENTITY(1,1)
    ,MyValue INT
)
GO

ALTER TABLE Test1 
    ADD CONSTRAINT PK_ID PRIMARY KEY (ID);
GO

CREATE TABLE Test2
(
    ID	INT NOT NULL
    ,SomeOtherValue INT
)

ALTER TABLE Test2 
    ADD CONSTRAINT PK_ID2 PRIMARY KEY (ID);
GO

ALTER TABLE Test2 
    ADD CONSTRAINT FK_ID FOREIGN KEY (ID)
    REFERENCES Test1 (ID);
GO


INSERT INTO Test1(MyValue) VALUES (9)
GO 1000

SELECT * FROM Test1
GO

DELETE FROM Test1
GO

DBCC CHECKIDENT(Test1, RESEED,0)
GO

INSERT INTO Test1 (MyValue) VALUES (8)
GO

SELECT * FROM Test1
GO

DROP TABLE Test2
GO
DROP TABLE Test1
GO

This was the dbcc output:

Beginning execution loop Batch execution completed 1000 times. Checking identity information: current identity value '1000', current column value '0'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Carlos SFC avatar image
Carlos SFC answered

Please try with this code.

Run it step by step. You neet to disble identity column for a moment.

-- Create test table
CREATE TABLE Test
(
id int identity,
obs varchar(50)
)

--Verify the identity column
INSERT INTO Test (obs) VALUES ('Probando id')
GO 
--Look at the id value
SELECT * FROM Test  ORDER BY id
GO
--Now we need to insert more values for id
INSERT INTO Test (obs) VALUES ('Se inserta del #2 al #51')
GO 50 
--Look at the id value
SELECT * FROM Test  ORDER BY id
GO
--DELETE 10 records of the table
DELETE FROM test WHERE id between 30 and 40
GO
-- Number of records 51 - 10
SELECT COUNT(*) FROM Test 
GO
-- id's 30 to 40 were deleted
SELECT * FROM Test  ORDER BY id
GO
-- verify the identity actual value
DBCC CHECKIDENT ("Test", NORESEED);
GO
-- Disable identity insert
/*******************************/
set identity_insert Test on
GO
-- INSERT id values 30 and 31
INSERT INTO Test (id,obs) VALUES (30,'Se inserta del #2 al #51')
GO
INSERT INTO Test (id,obs) VALUES (31,'Se inserta del #2 al #51')
GO
-- Enable identity value
set identity_insert Test off
GO
-- verify records
SELECT * FROM Test ORDER BY id
GO
-- verify value for the identity column
INSERT INTO Test (obs) VALUES ('Probando id #52')
GO
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.