Site Search:
Sign in | Join | Help
4Penny.net

SQL Server (T-SQL)

Comments and notes on SQL Server 2000, 2005, and T-SQL

Code to reset the identity seed value in SQL Server

[/code language="sql"]

--drop and create a table
drop table t6
CREATE TABLE t6(id int IDENTITY);
INSERT t6 DEFAULT VALUES;
INSERT t6 DEFAULT VALUES;
INSERT t6 DEFAULT VALUES;
INSERT t6 DEFAULT VALUES;
INSERT t6 DEFAULT VALUES;
INSERT t6 DEFAULT VALUES;

--look at the result of our handiwork
select * from t6
select ident_current( 't6')

--delete two lines, and look again
delete t6 where id = 6
delete t6 where id = 5
select * from t6

--reset the seed, and look again
declare @intID int
select @intID = max(id)  from t6
DBCC CHECKIDENT('t6', RESEED, @intID)
INSERT t6 DEFAULT VALUES;
select * from t6

[/code]

Comments

No Comments

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.