Search

Monday, July 9, 2012

Add or Modify IDENTITY property to an existing column

Method 1
Easiest method is to use SSMS to change the property.
  • Goto SQL Server Management Studio
  • Right click on the table name
  • Choose Design/Modify
  • Choose the column which needs to be set as Identity
  • At the bottom window (Column Properties) for that column you can see "Identity Specification" has been by default set to "NO".
  • Just change it to "YES" and save the table (CTRL + S).
Method 2

If there is no data in the table then use this method.

Drop the already exist column 
ALTER TABLE TempTable DROP Column ID;
GO;
Now add the ID column again with Identity specification on.
Alter Table TempTable Add ID INT IDENTITY
GO 

Method 3

If there is data in the table then use this method.
  • Create a new table with identity column
  • Enable IDENTITY_INSERT for this new table
  • Move the data from this old table to this new table.
  • Disable IDENTITY_INSERT for this new table
  • Delete the old table.
  • Rename the newtable with the oldtable name. 

No comments:

Post a Comment