Professional Documents
Culture Documents
Populate SQL Server Column With Sequential Number Without Identity
Populate SQL Server Column With Sequential Number Without Identity
com/) MENU
Problem
I have a database table that has a lot of data already in the table and I need to add a new column to this table to include a new
sequential number. In addition to adding the column, I also need to populate the existing records with an incremental counter what
options are there to do this?
Solution
The first approach that may come to mind is to add an identity column to your table if the table does not already have an identity
column. We will take a look at this approach as well as looking at how to do this with a simple UPDATE statement.
/
ALTER TABLE accounts ADD id INT IDENTITY(1,1)
GO
The statistics time and statistics i/o show this did about 23K logical reads and took about 48 seconds to complete.
/
After the table has been created and the data loaded we add a new INT column to the table that is not an identity column.
In this step we are doing an UPDATE to the table and for each row that is updated we are updating the variable by 1 as well as the id
column in the table. This can be seen here (SET @id = id = @id + 1) where we are making the @id value and the id column equal to
the current @id value + 1.
Below we can see the results where each record gets an incrementing value by 1.
/
The statistics time and statistics i/o show this did about 26K logical reads and took about 4.8 seconds to complete.
If we compare the statistics time and statistics i/o from the the update with the identity column to this approach the overall number of
logical reads is just about the same, but the overall duration is about 10 times faster doing the update versus having to maintain the
identity value.
For clarity purposes, I am going to first make the id column NULL for all records and then do the update.
BEGIN TRANSACTION
DECLARE @id INT
SET @id = 0
UPDATE accounts2
SET @id = id = @id + 1
COMMIT TRANSACTION
/
-- update rows using a CTE - Ervin Steckl
;WITH a AS(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) as rn, id
FROM accounts2
)
UPDATE a SET id=rn
OPTION (MAXDOP 1)
Summary
Once you have created an identity column there is no easy way to renumber all of your values for each row. With the update
approach you could do this over and over again by just rerunning the query and changing the values.
Next Steps
If you have the need to add a new sequential value to your tables or have the need to update an existing value in a sequential
manner don't forget about this approach
Look for other T-SQL tips and tricks here (/sql-server-tip-category/23/t-sql/)
Related Resources
Populate a SQL Server column with a sequential num... (/sqlservertip/1467/populate-a-sql-server-column-with-a-sequential-
number-not-using-an-identity/)
Generating On Demand Sequential Values in SQL Serv... (/sqlservertip/1293/generating-on-demand-sequential-values-in-sql-
server/)
More Database Developer Tips... (/sql-server-developer-resources/)