AcAnanth
AcAnanth

Reputation: 775

Updating Identity Column of a table with consecutive numbers through SQL Stored Procedure

After deleting the duplicate records from the table, I want to update Identity column of a table with consecutive numbering starting with 1. Here is my table details

id(identity(1,1)),
EmployeeID(int),
Punch_Time(datetime),
Deviceid(int)

enter image description here

I need to perform this action through a stored procedure. When i tried following statement in stored procedure

DECLARE @myVar int
        SET @myVar = 0
        set identity_insert TempTrans_Raw# ON
        UPDATE  TempTrans_Raw# SET  @myvar = Id = @myVar + 1
        set identity_insert TempTrans_Raw# off

gave error like...Cannot update identity column 'Id' Anyone please suggest how to update Identity column of that table with consecutive numbering starting with 1.

Upvotes: 2

Views: 1975

Answers (4)

Thorsten Kettner
Thorsten Kettner

Reputation: 94884

The IDENTITY keword is used to generate a key which can be used in combination with the PRIMARY KEY constraint to get a technical key. Such keys are technical, they are used to link table records. They should have no other meaning (such as a sort order). SQL Server does not guarantee the generated IDs to be consecutive. They do guarantee however that you get them in order. (So you might get 1, 2, 4, ..., but never 1, 4, 2, ...)

Here is the documentation for IDENTITY: https://msdn.microsoft.com/de-de/library/ms186775.aspx.

Personally I don't like it to be guaranteed that the generated IDs are in order. A technical ID is supposed to have no meaning other then offering a reference to a record. You can rely on the order, but if order is information you are interested in, you should store that information in my opinion (in form of a timestamp for example).

If you want to have a number telling you that a record is the fifth or sixteenth or whatever record in order, you can get always get that number on the fly using the ROW_NUMBER function. So there is no need to generate and store such consecutive value (which could also be quite troublesome when it comes to concurrent transactions on the table). Here is how to get that number:

select
  row_number() over(order by id),
  employeeid,
  punch_time,
  deviceid
from mytable;

Having said all this; it should never be necessary to change an ID. It is a sign for inappropriate table design, if you feel that need.

Upvotes: 1

Henrik Staun Poulsen
Henrik Staun Poulsen

Reputation: 13864

If you really need sequential numbers, may I suggest that you create a table ("OrderNumbers") with valid numbers, and then make you program pick one row from OrderNumbers when you add a row to yourTable.

If you everything in one transaction (i.e. with Begin Tran and Commit) then you can get one number for one row with no gabs.

You should have either Primary Keys or Unique Keys on both tables on this column to protect against duplicates.

HIH, Henrik

Upvotes: 1

Raj
Raj

Reputation: 10843

--before running this make sure Foreign key constraints have been removed that reference the ID. 

--insert everything into a temp table
SELECT (ColumnList) --except identity column
INTO #tmpYourTable
FROM yourTable

--clear your table
DELETE FROM yourTable
-- reseed identity
DBCC CHECKIDENT('table', RESEED, new reseed value)
--insert back all the values 
INSERT INTO yourTable (ColumnList)
SELECT OtherCols FROM #tmpYourTable
--drop the temp table
DROP TABLE #tmpYourTable
GO

Upvotes: 5

Thanos Markou
Thanos Markou

Reputation: 2623

Check this function: DBCC CHECKIDENT('table', RESEED, new reseed value)

Upvotes: -1

Related Questions