Solved

How to use Alter table to set Identity column?

Posted on 2003-11-18
5
968 Views
Last Modified: 2011-09-20
Kindly specify how to use Alter Table to set the Identity Column?
0
Comment
Question by:muktamariwala
  • 2
5 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 9776668
ALTER TABLE MyTable
Add MyColumn [int] identity (1, 1) not null

Of course, you cannot change the identity column in this manner once it has been created - this will only allow you to set it on a table that does not already have an identity column.

Cheers
Night
0
 

Author Comment

by:muktamariwala
ID: 9776744
but can I associate the Identity to a column which is alreday in the table???
0
 
LVL 29

Accepted Solution

by:
Nightman earned 20 total points
ID: 9777147
No. You cannot alter an existing column using transact SQL and make it an identity column.

However, you can do what the SQL Enterprise Damager does - create a new temporary table with the structure that you require, set IDENTITY_INSERT ON, insert your data from the old table into the temporary table, drop the old table and rename the new table to have the same name as the old table had.

Of course, this could be a problem if the old table has foreign keys / constraints, triggers, etc - you will have to drop those and apply them to the new table after it has been built.

Ensure that the identity seed is set to the max value of the column - you don't want errors when SQL attempts to create a value that already exists.

Suggest you do all of this in a single transaction - rollback on error.

Also, backup your database first.

Cheers
Night
0
 
LVL 17

Expert Comment

by:walterecook
ID: 10392120
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: Nightman {http:#9777147}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

walterecook
EE Cleanup Volunteer
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now