Alter table set Identity column

大城市里の小女人 提交于 2019-12-10 18:38:36

问题


I have a sybase table ("Test") having the following columns:

MyIdentity numberic(9,0)
Name
User

The table is filled with lots of records. I want to alter MyIdentity column to be an identity.

There are currently no duplicate values for MyIdentity in the table. How can i alter the table and set MyIdentity as identity?


回答1:


Apparently you can't, something like

Create a new Table called test2 with the identity set up

Then

Set Identity_Insert Test2 On

Insert Test2 Select * From Test

Set Identity_Insert Test2 Off

Drop Test and Rename Test 2...



回答2:


At least on my version (Adaptive Server Enterprise/15.0.3) the following seems to do the trick (syntactic sugar omitted):

Add a new identity column:

alter table Test add newMyIdentity numeric(9,0) identity not null

Prepare for updates:

set identity_insert Test on
set identity_update Test on

Copy the existing ID (to be able to preserve foreign keys etc):

update Test set newMyIdentity=MyIdentity

Drop the old definition, rename the newly created column:

alter table Test drop MyIdentity
sp_rename "Test.newMyIdentity", MyIdentity

Clean up:

set identity_insert Test off
set identity_update Test off
sp_recompile Test



回答3:


Just use:

create index MyIdentity on Test

See http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@Generic__BookTextView/36137;pt=36137.



来源:https://stackoverflow.com/questions/9571249/alter-table-set-identity-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!