Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use Alter table to set Identity column?

Posted on 2003-11-18
5
Medium Priority
?
1,030 Views
Last Modified: 2011-09-20
Kindly specify how to use Alter Table to set the Identity Column?
0
Comment
Question by:muktamariwala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 80 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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