Creating a Sequential Record Number field

By Bill Graziano on 25 September 2000 | 13 Comments | Tags: UPDATE


Ok, this has to be one of the coolest things I've discoverd about SQL Server in quite a while. It is a way to create a sequential record number field on a table using a single update statement. Until I discovered this, I would have said this was impossible. Read on for the solution.

While digging around in SQL Server recently I discovered something very interesting. There is a quote from Books Online for the UPDATE statement that says:

SET @variable = column = expression sets the variable to the same value as the column. This differs from SET @variable = column, column = expression, which sets the variable to the pre-update value of the column.
This led me to believe I might be able to use a single update statement to increment a value in a table. Before this trick, if you wanted to create a sequential record number you had two bad choices. You could create a cursor and move through the records, setting the values one at a time to counter value that you incrememented. That was slow and not a set-oriented operation like SQL Server likes.

The second option was to create a temp table with an identity column and a column for the primary key of the table you wanted to number. Insert all the records into the temp table and the temp table identity column becomes the sequential record counter. You can join back to the original table and update the sequential record number. The only reason you'd still choose this option is if you want a custom sort order. This little trick won't give you that.

This little piece of code will run through a table and sequentially number the field you specify. The only drawback is that it will determine the order based on the physical order of the table.

declare @intCounter int
set @intCounter = 0
update Yaks
SET @intCounter = YakSequenceNumber = @intCounter + 1


After running this statement, the field YakSequenceNumber will hold nicely incremented values.

Discuss this article: 13 Comments so far. Print this Article. This page has been read 82,242 times.

If you like this article you can sign up for our newsletter. We send it out each week that we post a new article. There's an opt-out link at the bottom of each newsletter so it's easy to unsubscribe at any time.

Email Address:

Email ThisSubscribe to this feedKick itSave to del.icio.usView blog reactions

Related Articles

Using REPLACE in an UPDATE statement (20 January 2002)

Other Recent Forum Posts

SQL Newbie with a join question (2 Replies)

converting time question (4 Replies)

How to fix a table with a single bad record/index (6 Replies)

Update using two tables (3 Replies)

sub data in table (16 Replies)

Sample database (5 Replies)

foreign key problem.. (6 Replies)

Joins within and scross tables (5 Replies)

Subscribe to SQLTeam.com

Weekly SQL Server newsletter with articles, forum posts, and blog posts via email:

SQLTeam.com Articles via RSS

SQLTeam.com Weblog via RSS

- Advertisement -

- Sponsor's Message -

SQLShare.com Videos

Renaming a Database

You won't do it often, but it's nice to know how, and you're not still using sp_renamedb are you? Join us for a quick look at how to use the alter syntax to change the db name along with a tip on how to quickly disconnect any remaining users from the database.

File Share Subscriptions in Reporting Services

Whether you want to generate PDF invoices for customers or do a daily export that will get processed by one of your vendors, the ability to deliver reports to a file share is a useful and simple feature baked in to Reporting Services. In this lesson Devin will show you how to do it and how to set most of the common options.

File Share Subscriptions in Reporting Services

Whether you want to generate PDF invoices for customers or do a daily export that will get processed by one of your vendors, the ability to deliver reports to a file share is a useful and simple feature baked in to Reporting Services. In this lesson Devin will show you how to do it and how to set most of the common options.

Using DB_ID and DB_Name Functions

Simple but effective, DB_ID and DB_Name give you a concise way to look up the id of a database from a name, or look up the name of a database from an id. There are times when you'll need to write the join to sys.sysdatabases, but when all you need is a quick conversion, these functions get it done.

Using @@Total_Read and @@Total_Write

Ever wondered how many physical reads and writes your SQL service is doing? Yes, it may seek a bit geeky, but it's often useful to have an idea if a server is read or write heavy. @@Total_Read and @@Total_Write show you the number of physical (not logical) reads since the last service restart, and from there we can easily calculate a percentage of reads if needed.