Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Reseeding An Identity Column In A Table Variable

DZone's Guide to

Reseeding An Identity Column In A Table Variable

·
Free Resource
Closest thing to reseeding a T-SQL Table Variable.
If it was a real Table or a Temporary Table, you could use TRUNCATE TABLE or DBCC CHECKIDENT, but those do not work on Table Variables. Only works on SQL Server 2005 or later (needs OVER semantic).


DECLARE @t table (offset int IDENTITY(1,1), foo nvarchar(50))
INSERT INTO @t (foo) VALUES ('one')
INSERT INTO @t (foo) VALUES ('two')
INSERT INTO @t (foo) VALUES ('three')
SELECT row_number() OVER (ORDER BY offset), foo FROM @t
DELETE FROM @t
INSERT INTO @t (foo) VALUES ('four')
SELECT row_number() OVER (ORDER BY offset), foo FROM @t
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}