Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 how to alter column to be identity

Author  Topic 

michaelxvo
Starting Member

47 Posts

Posted - 2006-04-26 : 11:48:44
I have a table with a column: column1 int primary key. I would like to alter this column to be identity.
how do I write in T-SQL:
alter table test
alter column1 (I get stuck here)

thank you

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-04-26 : 13:37:05
You can't use ALTER TABLE to modify a column to be identity. Perform the change in Enterprise Manager but prior to saving it, click the save change script button. Check out the code. That's the same code that would be required if you were to write the code by yourself in Query Analyzer. So what you have to do is:

1. create a new table with the desired layout
2. move your data into this new table
3. drop your old table
4. rename the new table to the old table
5. add back any constraints and/or indexes

Tara Kizer
aka tduggan
Go to Top of Page

michaelxvo
Starting Member

47 Posts

Posted - 2006-04-26 : 13:54:53
I can do the change to be identity with EM.
The problem is this:
The old table has column1 identity value.
I migrate to the new table AND I WANT THE NEW TABLE to KEEP the old identity values and CONTINUE the IDENTITY VALUES FROM the old table. In order for me to do that, I create the new table without identity and migrate data to the new table.
after migrating the data, now I want to activate the that old identity on column1
Go to Top of Page

michaelxvo
Starting Member

47 Posts

Posted - 2006-04-26 : 13:55:18
I can do the change to be identity with EM.
The problem is this:
The old table has column1 identity value.
I migrate to the new table AND I WANT THE NEW TABLE to KEEP the old identity values and CONTINUE the IDENTITY VALUES FROM the old table. In order for me to do that, I create the new table without identity and migrate data to the new table.
after migrating the data, now I want to activate the that old identity on column1
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-04-26 : 14:00:50
Please see my steps on how to do this in Query Analyzer. You just need to translate the steps into code. Enterprise Manager can generate this code for you as mentioned in my first post.

Tara Kizer
aka tduggan
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-04-26 : 14:39:21
Forgot one thing. When you copy your data from the old table into the new table, you'll need to use SET IDENTITY_INSERT ON.

SET IDENTITY_INSERT NewTable ON

INSERT INTO NewTable (Column1, ...)
SELECT Column1, ...
FROM OldTable

SET IDENTITY_INSERT NewTable OFF

Tara Kizer
aka tduggan
Go to Top of Page

michaelxvo
Starting Member

47 Posts

Posted - 2006-04-26 : 16:00:01
thank you very much for your code.

Go to Top of Page

cjohnson6
Starting Member

1 Post

Posted - 2006-05-04 : 15:23:03
Can this same procedure be done with SQL Server 2005? I can't seem to recreate this as SQL 2005 now uses that "SQL Server Mgmt Studio" utility. Basically the same concept as Enterprise Mgr but not quite. Any thoughts?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-05-04 : 15:28:22
cjohnson6,

Please post what code you are running in Management Studio.

Tara Kizer
aka tduggan
Go to Top of Page

sampro
Starting Member

4 Posts

Posted - 2008-01-22 : 10:36:31
i m using sqlserver 2005
i have a table that has the primary key set as identity
i want to make that off insert one row and then again make that on and i want to do that through sql script.

please can anyone specify it i need that urgently
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-22 : 10:43:28
Please don't hijack threads.
You already posted and got an answer here
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=95938



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -