How can I use a script to change the auto-increment column of a table to a non-auto-increment column?

Source: Internet
Author: User

-- Create a test table
[Create] Table T1 (ID int identity, a INT)
Go
-- Insert record
Insert T1 values (1)
Go
-- 1. Change the identity column to a normal column.
[Alter] Table T1 add id_temp int
Go
Update T1 set id_temp = ID
[Alter] Table T1 drop column ID
Exec sp_rename n 'T1. id_temp ', N 'id', N 'column'
Insert T1 values (100,9)
Go
-- 2. change a common column to an ID column
[Create] Table t1_temp (ID int, A int identity)
Set identity_insert t1_temp on
Insert t1_temp (ID, a) Select * from T1
Set identity_insert t1_temp off
Drop table T1
Go
Exec sp_rename n 'T1 _ temp ', N 't1'
Insert T1 values (109999)
Go
-- Display the processing result
Select * from T1
/* -- Result:
ID
----------------------------
1 1
100 9
109999 10
--*/
I marked "CREATE" and "alter" to upload data. You only need to delete the test!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.