RESEED Identity Column in Database Table – Rest Table Identity Value – SQL in Sixty Seconds

Identity Column in Database

Syntax

DBCC CHECKIDENT
(
table_name
[, { NORESEED | { RESEED [, new_reseed_value ] } } ]
)
[ WITH NO_INFOMSGS ]

table_name
Is the name of the table for which to check the current identity value. The table specified must contain an identity column. Table names must follow the rules for identifiers. Two or three part names must be delimited, such as ‘Person.AddressType’ or [Person.AddressType].

NORESEED
Specifies that the current identity value shouldn’t be changed.

RESEED
Specifies that the current identity value should be changed.

new_reseed_value
Is the new value to use as the current value of the identity column.

WITH NO_INFOMSGS
Suppresses all informational messages.

Caller must own the schema that contains the table, or be a member of the sysadmin fixed server role, the db_owner fixed database role, or the db_ddladmin fixed database role.

Here is the script :

USE tempdb
GO
-- Create Table
CREATE TABLE TestTable (ID INT IDENTITY(1,1), Col1 VARCHAR(100));
-- Insert Table
INSERT INTO TestTable (Col1)
SELECT 'First'
UNION ALL
SELECT 'Second'
UNION ALL
SELECT 'Third'
UNION ALL
SELECT 'Fourth'
UNION ALL
SELECT 'Fifth';
-- Select data
SELECT *
FROM TestTable
GO
-- Returns current Identity Value
DBCC CHECKIDENT ('TestTable', NORESEED);
-- Resets the current Identity value to maximum value
DBCC CHECKIDENT ('TestTable', RESEED, 11);
-- Insert Table
INSERT INTO TestTable (Col1)
SELECT 'First'
UNION ALL
SELECT 'Second'
UNION ALL
SELECT 'Third'
UNION ALL
SELECT 'Fourth'
UNION ALL
SELECT 'Fifth';
-- Select Table
SELECT *
FROM TestTable
ORDER BY ID;
-- Drop Table
DROP TABLE TestTable;

A. Resetting the current identity value, if it’s needed
The following example resets the current identity value, if it’s needed, of the specified table in the AdventureWorks2012 database.

SQL

USE AdventureWorks2012;
GO
DBCC CHECKIDENT (‘Person.AddressType’);
GO

B. Reporting the current identity value
The following example reports the current identity value in the specified table in the AdventureWorks2012 database, and doesn’t correct the identity value if it’s incorrect.

SQL

USE AdventureWorks2012;
GO
DBCC CHECKIDENT (‘Person.AddressType’, NORESEED);
GO

C. Forcing the current identity value to a new value
The following example forces the current identity value in the AddressTypeID column in the AddressType table to a value of 10. Because the table has existing rows, the next row inserted will use 11 as the value – the new current identity value defined for the column plus 1 (which is the column’s increment value).

SQL

USE AdventureWorks2012;
GO
DBCC CHECKIDENT (‘Person.AddressType’, RESEED, 10);
GO

D. Resetting the identity value on an empty table
The following example forces the current identity value in the ErrorLogID column in the ErrorLog table to a value of 1, after deleting all records from table. Because the table has no existing rows, the next row inserted will use 1 as the value, that is, the new current identity value, without adding the increment value defined for the column.

SQL

USE AdventureWorks2012;
GO
TRUNCATE TABLE dbo.ErrorLog
GO
DBCC CHECKIDENT (‘dbo.ErrorLog’, RESEED, 1);
GO


Discover more from CODE t!ps

Subscribe to get the latest posts sent to your email.

Scroll to Top

Discover more from CODE t!ps

Subscribe now to keep reading and get access to the full archive.

Continue reading