Skip to content
Advertisement

Alter table to add incrementally primary key records where ID values are null

I have a table in sql server with ID field as the primary key. In the rows of the ID field, some have primary key values while some rows do not have primary key values because the ID column allows null. Now I want to run a query to insert values incrementally into the rows that are null so that they can have primary key values. I have tried using an ALTER command but no head way

Advertisement

Answer

You can DROP that column and ADD again with Auto Increment value.

ALTER TABLE your_table DROP COLUMN ID
ALTER TABLE your_table ADD ID INT IDENTITY(1,1)

This will generate all values from the start and as a result you will lose existing value (upto 6).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement