问题
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