Change the table column for SQL Server from non-self-growth to self-growing

Source: Internet
Author: User

Reprint: http://www.thinksaas.cn/topics/0/423/423869.html

Demo

/**************** Preparation Environment ********************/--determine if a test table existsifOBJECT_ID (N'Test'N'U') isNotNULLdrop table Test--Creating the Test Table CREATE TABLE test (IDintNotNULL, name varchar ( -) notNULL)--Insert temporary data insert into test values (1,'Jackie Chan') insert into test values (3,'Zhang Ziyi') insert into test values (4,'Rene') insert into test values (8,'Faye Wong')Select* fromTest/**************** Implementing the Change auto-grow column ********************/begin transactioncreate Table test_tmp (IDintNotNULLIdentity1,1), name varchar ( -) notNULL) GoSetidentity_insert test_tmp OngoifExistsSelect* fromtest) EXEC ('INSERT into test_tmp (ID, name) Select ID, name from test with (Holdlock Tablockx)') GoSetidentity_insert test_tmp offgodrop table testgoexec sp_rename N'test_tmp'N'Test','OBJECT'Gocommitgo/**************** Validation Results *****************/INSERT into test values ('Zhang Man')Select* fromTest

Instance:

/**************** Implementing the Change auto-grow column ********************/begin transactioncreate Table test_tmp (UsergradeidintNotNULLIdentity1,1), Usergrade nvarchar (8) notNULL, [Status]intNotNULL, Remark nvarchar ( -), AddUser nvarchar ( +), Upduser nvarchar ( +), Addtime datetime2 (7), Updtime datetime2 (7)) GoSetidentity_insert test_tmp OngoifExistsSelect* fromm_usergrade) EXEC ('INSERT INTO test_tmp (usergradeid,usergrade,[status],remark,adduser,upduser,addtime,updtime)SelectUsergradeid,usergrade,[status],remark,adduser,upduser,addtime,updtime fromM_usergrade with (Holdlock Tablockx)')GoSetidentity_insert test_tmp offgodrop table m_usergradegoexec sp_rename N'test_tmp'N'M_usergrade','OBJECT'Gocommitgo

Change the table column for SQL Server from non-self-growth to self-growing

Related Article

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.