Translate

Wednesday, August 18, 2021

TSQL - Add a Sequential Number Field

I needed to insert records from one table into another table and add a sequential number that started at a certain value. This was the eastiest way that I found to add the number. Just add an int field to the source table and add the values using an update. So in the following code, id was the int field that I created, and I add numbers begining with 6001.

DECLARE @id INT 
SET @id = 6000
UPDATE Table 
SET @id = id = @id + 1 

No comments:

Post a Comment

Thank you for commenting!