/**************** 準備環境********************/
--判斷是否存在test表
if object_id(N'test',N'U') is not null
drop table test
--創建test表
create table test
(
id int not null,
name varchar(20) not null
)
--插入臨時數據
insert into test values (1,'成龍')
insert into test values (3,'章子怡')
insert into test values (4,'劉若英')
insert into test values (8,'王菲')
select * from test
/**************** 實現更改自動增長列********************/
begin transaction
create table test_tmp
(
id int not null identity(1,1),
name varchar(20) not null
)
go
set identity_insert test_tmp on
go
if exists(select * from test)
exec(' insert into test_tmp(id, name ) select id, name from test with(holdlock tablockx)')
go
set identity_insert test_tmp off
go
drop table test
go
exec sp_rename N'test_tmp' ,N'test' , 'OBJECT'
go
commit
GO
/****************驗證結果*****************/
insert into test values ('張曼')
select * from test