Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 rearrange id column

Author  Topic 

learning_grsql
Posting Yak Master

230 Posts

Posted - 2013-06-01 : 18:12:21
I have columns similar to below.As you see below, when rows are deleted the id nos. are not in series. How can I rearrange it to 1,2,3,4,5 again?
[CODE]
id | column1
1 | values
2 | values
5 | values
6 | values
10 | values
[/CODE]

pascal_jimi
Posting Yak Master

167 Posts

Posted - 2013-06-01 : 18:19:26

CREATE TABLE TableID (ID INT IDENTITY(1,1), Col VARCHAR(10))
GO
/* Insert 10 records with first value */
INSERT INTO TableID (Col)
VALUES ('First')
GO 10
/* Check the records in table */
SELECT *
FROM TableID
GO
/* Delete last few records */
DELETE
FROM TableID
WHERE ID IN (8,9,10)
GO
/* Check the records in table */
SELECT *
FROM TableID
GO
/* Get current Max Value and reseed table */
DECLARE @MaxID INT
SELECT @MaxID = MAX(ID)
FROM TableID
DBCC CHECKIDENT('TableID', RESEED, @MaxID)
GO
/* Insert 10 records with second value */
INSERT INTO TableID (Col)
VALUES ('Second')
GO 5
/* Check the records in table */
SELECT *
FROM TableID
GO
/* Clean Database */
DROP TABLE TableID
GO

kmkmmm
Go to Top of Page

pascal_jimi
Posting Yak Master

167 Posts

Posted - 2013-06-01 : 18:20:01
http://blog.sqlauthority.com/2009/04/01/sql-server-reseed-identity-of-table-table-missing-identity-values-gap-in-identity-column/

kmkmmm
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-01 : 20:21:14
see

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=185740

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -