Solved

How to use Alter table to set Identity column?

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
This article describes some very basic things about SQL Server filegroups.
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…

776 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