请问在一个表中插入新列并填充数据的有关问题
请教在一个表中插入新列并填充数据的问题
我在一个现有表(T1)中新增一个ID字段,设定为自动增长,然后根据表T1中的“编号”字段的顺序来为ID字段填入数据,在查询管理器中这个语句应该怎样写啊。(表中共有10000条记录)
请懂的人帮帮忙。谢谢啦!
------解决方案--------------------
如果你原来表中的数据的存储顺序是按编号列增大的顺序,则:
我在一个现有表(T1)中新增一个ID字段,设定为自动增长,然后根据表T1中的“编号”字段的顺序来为ID字段填入数据,在查询管理器中这个语句应该怎样写啊。(表中共有10000条记录)
请懂的人帮帮忙。谢谢啦!
------解决方案--------------------
如果你原来表中的数据的存储顺序是按编号列增大的顺序,则:
- SQL code
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
------解决方案--------------------
- SQL code
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;
------解决方案--------------------
- SQL code
alter table t1 add id identity(int,1,1) update t1 set id=编号
------解决方案--------------------
- SQL code
alter table ta add id int not null identity(1,1)
------解决方案--------------------
- SQL code
alter table t1 add id identity(1,1) update t1 set id=编号
------解决方案--------------------
如果原表中存储的物理顺序并非按编号排列的,则:
- SQL code
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
------解决方案--------------------
- SQL code
alter table t1 add id int identity(1,1) update t1 set id=编号