Solved

How to use Alter table to set Identity column?

Posted on 2003-11-18
5
981 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

911 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

25 Experts available now in Live!

Get 1:1 Help Now