Recently I needed to add an Identity column to a table which is already populated. Of course, adding a new column to a table with identity property can be done easily. But the problem is to generate values for the existing records in a custom order, as the values are getting generated automatically according to the order of the clustered index of the table.
So how do you add an identity column to a table and get the identity values generated for the existing records in a custom order? To Understand the problem let's create a table and populate it with some records.
Here a clustered index has been created on the InsertedDate column. Now let's populate it with some records.
And the results are as follows.
It is clear that records are getting ordered according to the InsertedDate column values due to the clustered index. Now let's add an identity column to the table and see how values are getting generated.
As shown, identity values for existing records get generated in the order of the clustered index. How do we get them according to the order of the FirstName?
The method is to first load the records to a temporary table and truncate the table. and enable IDENTITY_INSERT property of the table ON. Then load the table according to the required order and set it back to OFF.
The method is to first load the records to a temporary table and truncate the table. and enable IDENTITY_INSERT property of the table ON. Then load the table according to the required order and set it back to OFF.
In this method, it is essential to use TRUNCATE TABLE command instead of DELETE as other than deleting the records, resetting the identity property must also be done before inserting the ordered records.
If you can add the identity column in the middle of the data modification, there is no need to use this approach. But in my case, we were needed to separate the schema changes and data modifications into different files.
Full script of the above code can be downloaded here.