SQL SERVER – How to an Add Identity Column to Table in SQL Server

Here is the question I received on SQLAuthority Fan Page.

“How do I add an identity column to Table in SQL Server? “

Sometime the questions are very very simple but the answer is not easy to find.

Scenario 1:

If you are table does not have identity column, you can simply add the identity column by executing following script:

ALTER TABLE MyTable
  ADD ID INT IDENTITY(1,1) NOT NULL

Scenario 2:

If your table already has a column which you want to convert to identity column, you can’t do that directly. There is a workaround for the same which I have discussed in depth over the article Add or Remove Identity Property on Column.

Scenario 3:

If your table has already identity column and you can want to add another identity column for any reason – that is not possible. A table can have only one identity column. If you try to have multiple identity column your table, it will give following error.

Msg 2744, Level 16, State 2, Line 2
Multiple identity columns specified for table ‘MyTable‘. Only one identity column per table is allowed.

Leave a comment if you have any suggestion.

Reference : Pinal Dave (https://blog.sqlauthority.com)

Previous Post
SQL SERVER – Data Sources and Data Sets in Reporting Services SSRS
Next Post
SQL SERVER – Weekly Series – Memory Lane – #039

Related Posts

7 Comments. Leave new

  • i) create another column as uniqueidentifier default newid()
    ii) Or populate another column manually(maintain sequence),by creating function which will fetch last value manipulate that value and return new value.
    Or sql server 2012 new feature Sequence can be use

    Reply
  • how to add decimal values in identity like .1;.2;.3 etc

    Reply
  • Even sequence in sql server won’t generate value like .1,.2,.3 etc.
    so i think you hv to define UDF to achieve so.either do it manually or make that column as computed .

    Reply
  • Barry Seymour
    May 7, 2015 5:43 am

    I use this to give a RowID to a table I imported using BULK INSERT. I then query my table for certain fields (i.e., Column1 like ‘____.____%’ to seek account numbers) but I get different values every time, even with the same file!

    Are there hidden traps to this technique? It is TOTALLY not working for me.

    See for the details.

    Reply
  • Michael McInnis
    October 23, 2019 11:33 pm

    Hey Buddy! I just want to give you a shout out for all your hard work here on line. I’ve used your tips for years and want to thank you for your clear, concise examples. All the Best!

    Reply
  • Priya varma
    May 7, 2020 8:32 pm

    Can we Create Value like (ID varchar IDENTITY(A1000,1)).kindly Anyone reply

    Reply

Leave a Reply

Menu