select id=identity(int,1,1),[除identity字段外的列] into new from old
1、如果只有旧数据,新数据未插入表中 truncate table 表名 执行后,再插入新数据,自增列又从1开始了2、如果表中有新旧数据,先删除旧数据,再执行下面语句 alter table 表名 drop column id alter table 表名 add id int identity不过这样自增ID就在最后一列了,但要比select id=identity(int),* into 新表 from 旧表 要快些也可以参考如何把新增列调整第一列的文章
--1、如果要清除所有数据,并将自增id从1开始。 truncate table 表名 --清除所有数据并将自增id重新初始化。 --2、如果要保留数据, select * into # from 表名 --将数据导入临时表 truncate table 表名 --清除表中数据,并初始化自增id insert into 表名(字段1,字段2) select 字段1,字段2 from # --将临时表中数据导回表中。
更新字段值自动增加;create table #t(id int ,name varchar(20)) insert into #t(name) select 'a' insert into #t(name) select 'a' insert into #t(name) select 'a' insert into #t(name) select 'a' insert into #t(name) select 'a' insert into #t(name) select 'a'declare @i int set @i=0update #t set @i=@i+1, id=@Iselect * from #t
from old
truncate table 表名
执行后,再插入新数据,自增列又从1开始了2、如果表中有新旧数据,先删除旧数据,再执行下面语句
alter table 表名 drop column id
alter table 表名 add id int identity不过这样自增ID就在最后一列了,但要比select id=identity(int),* into 新表 from 旧表 要快些也可以参考如何把新增列调整第一列的文章
--1、如果要清除所有数据,并将自增id从1开始。
truncate table 表名 --清除所有数据并将自增id重新初始化。
--2、如果要保留数据,
select * into # from 表名 --将数据导入临时表
truncate table 表名 --清除表中数据,并初始化自增id
insert into 表名(字段1,字段2) select 字段1,字段2 from # --将临时表中数据导回表中。
更新字段值自动增加;create table #t(id int ,name varchar(20))
insert into #t(name) select 'a'
insert into #t(name) select 'a'
insert into #t(name) select 'a'
insert into #t(name) select 'a'
insert into #t(name) select 'a'
insert into #t(name) select 'a'declare @i int
set @i=0update #t set @i=@i+1, id=@Iselect * from #t
(
'table_name'
[ , {
NORESEED | { RESEED [ , new_reseed_value ] }
}
]
)
[ WITH NO_INFOMSGS ]可以更改标示的
from old
delete from old
set identity_insert old on
insert into old select * from old
不是很麻烦啊!