Populating a database column with an incrementing value

Author: Steven Neiland

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.

SET @id = 0

UPDATE sometable
SET @id = id = @id + 1

What Do You Think?

Reader Comments

Comment Etiquette:

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain or appear to be advertisments, will not be published.
  • Comments that appear to be created for the purpose of linkbuilding to commercial sites will be removed.

Archives Blog Listing