Populating a database column with an incrementing value

Author: Steven Neiland

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

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

Reader Comments

  • 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 advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing