Populating a database column with an incrementing value

Published: {ts '2020-08-25 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/populating-a-database-column-with-an-incrementing-value/

Here's a handy sql snippet from my archives. This allows us to populate a database column with an incrementing value. I found this useful when I was forced to manually migrate a bunch a historical data from a weird proprietary system a couple of years ago.

What I find interesting about this snippet is how it simply demonstrates chaining of variable assignment as part of an update operation.

DECLARE @id INT SET @id = 0 UPDATE sometable SET @id = id = @id + 1