日期:2014-05-18 浏览次数:20434 次
create table t1(编号 int) insert into t1 select 1 union all select 5 union all select 19 union all select 22 go alter table t1 add id int identity(1,1) select * from t1 /* 编号 id ----------- ----------- 1 1 5 2 19 3 22 4 (4 行受影响) */ go drop table t1
------解决方案--------------------
create table #t(n varchar(10)); go insert into #t select 'a' union all select 'b' union all select 'c' union all select 'd'; --select * from #t; alter table #t add id int primary key identity(1,1); select * from #t;
------解决方案--------------------
alter table t1 add id identity(int,1,1) update t1 set id=编号
------解决方案--------------------
alter table ta add id int not null identity(1,1)
------解决方案--------------------
alter table t1 add id identity(1,1) update t1 set id=编号
------解决方案--------------------
如果原表中存储的物理顺序并非按编号排列的,则:
create table t1(编号 int) insert into t1 select 11 union all select 5 union all select 19 union all select 22 union all select 7 go select * into temp_t1 from t1 order by 编号 alter table temp_t1 add id int identity(1,1) drop table t1 go exec sp_rename 'temp_t1','t1' go select * from t1 /* 编号 id ----------- ----------- 5 1 7 2 11 3 19 4 22 5 (5 行受影响) */ go drop table t1
------解决方案--------------------
alter table t1 add id int identity(1,1) update t1 set id=编号